Loading MudBlazor components dynamically at runtime in .NET MAUI

Loading MudBlazor components dynamically at runtime in .NET MAUI

12 November 2025

Buy Me A Coffee

Hello! Today we will talk about a challenge that often arises when building enterprise-grade modular systems: Loading MudBlazor components dynamically at runtime in .NET MAUI.

In a standard .NET MAUI Hybrid application, your Razor components are compiled into the main assembly. But what if you need to build a "Plug-and-Play" architecture where a server can send a .dll containing new UI views, and your app renders them without a trip to the App Store? This is particularly useful for dashboards, dynamic forms, or white-labeling.

The Challenge: Why Reflection isn't enough

Loading an assembly via Assembly.Load is the easy part. The real difficulty in a Blazor Hybrid environment is that the RenderTree needs to know about the new components, and more importantly, MudBlazor services (like the DialogService or Snackbar) must be correctly injected into these external components.

1. Preparing the Plugin (The Razor Class Library)

Your plugin must be a Razor Class Library (RCL). To keep it compatible with MudBlazor, ensure it references the same version of the library as your host app.

// In your ExternalPlugin.dll
public partial class DynamicDashboard : ComponentBase
{
    [Inject] private ISnackbar Snackbar { get; set; }

    protected override void OnInitialized()
    {
        Snackbar.Add("Hello from a dynamic DLL!", Severity.Success);
    }
}

2. Loading the Assembly via AssemblyLoadContext

To avoid memory leaks and allow for potential unloading, we use a custom AssemblyLoadContext.

public class PluginLoadContext : AssemblyLoadContext
{
    public PluginLoadContext() : base(isCollectible: true) { }

    protected override Assembly Load(AssemblyName assemblyName)
    {
        // Prevent loading multiple versions of shared libraries like MudBlazor
        if (assemblyName.Name == "MudBlazor" || assemblyName.Name == "Microsoft.AspNetCore.Components")
            return null;

        return base.Load(assemblyName);
    }
}

3. Registering the Dynamic Component in the RenderTree

The "magic" happens in your Blazor page. Instead of a static component tag, we use the <DynamicComponent /> provided by ASP.NET Core.

@page "/dynamic-loader"
@using System.Reflection

<MudText Typo="Typo.h4">Plugin Loader</MudText>

<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="LoadPlugin">
    Load External UI
</MudButton>

@if (_componentType != null)
{
    <MudPaper Class="pa-4 mt-4">
        <DynamicComponent Type="_componentType" Parameters="_parameters" />
    </MudPaper>
}

@code {
    private Type _componentType;
    private Dictionary<string, object> _parameters = new();

    private async Task LoadPlugin()
    {
        // 1. Download or get the stream of your DLL
        var stream = await FileSystem.OpenAppPackageFileAsync("ExternalPlugin.dll");

        // 2. Load into context
        var context = new PluginLoadContext();
        var assembly = context.LoadFromStream(stream);

        // 3. Find the component by attribute or name
        _componentType = assembly.GetTypes()
            .FirstOrDefault(t => t.IsSubclassOf(typeof(ComponentBase)) && t.Name == "DynamicDashboard");

        StateHasChanged();
    }
}

4. Handling Dependency Injection

For MudBlazor components to work inside the dynamic DLL, they must share the same IServiceProvider instance. Since DynamicComponent uses the host’s container, your DialogService and SnackbarProvider will work out of the box, provided you have the <MudThemeProvider /> and providers initialized in your MainLayout.razor.

Summary

By combining AssemblyLoadContext and <DynamicComponent />, you can transform your .NET MAUI app into a flexible shell. This approach solves the problem of rigid UI structures and allows your MudBlazor interfaces to evolve independently of the core application logic.

Have you tried loading external assemblies in MAUI? Let’s discuss the potential pitfalls in the comments!

Buy Me A Coffee

Related:

Creating CalendarView with .NET MAUI Handler architecture

Explore the creation of a CalendarView control in .NET MAUI using Handler architecture.

Effortless Android Library Binding

Bind Java libraries directly from Maven repositories.

An unhandled error has occurred. Reload

🗙