Search code examples
.net-assemblyblazor-webassemblydynamic-import

Load external Assembly (RCL) to Blazor WebAssembly app


It is possible to load a RCL (Razor Component Library) to Blazor WebAssembly dynamically?

I found this Loading an external .NET Standard 2.0 assembly with blazor to load a standard classes

What I want is to develop a pluggable/extensible visual framework, where putting a dll in a ASP.NET Core Server folder where enought to access to that blazor component

Solution config:

  • ASP.NET Core WebAPI project
  • Blazor WebAssembly project
  • RCL project 1 with some components
  • RCL project 2 with another components

Steps:

  • Open a Blazor Page and OnInitializedAsync() retreive some dll from WebAPI as binary
  • Load binary to Assembly
  • Reference the assembly and use it dynamically in the page

Solution

  • I finally develop a solution I want to share with you. A Module Manager witch allows you to load any outside component dynamically

    Check it out here:

    https://github.com/elgransan/BlazorPluginComponents

    Some example code

            var componentPackage = "RazorClassLibrary2";
            var component = "Component2";
            var stream = await Http.GetStreamAsync($"{MyNavigationManager.BaseUri}/{componentPackage}/{componentPackage}.dll");
            var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
            componentType = assembly.GetType(componentPackage + "." + component);
            await DOMinterop.IncludeLink(componentPackage, $"/{componentPackage}/{componentPackage}.styles.css");
    

    Where the files are in the server