natemcmaster / DotNetCorePlugins

.NET Core library for dynamically loading code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Unable to load a Blazor application in a regular .Net Core 5 app

80O opened this issue · comments

commented

Hello,

I've been trying to load a blazor app in a regular .net core 5 app but keep getting the following exceptions whenever I try to load the plugin:

System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.AspNetCore.Components, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

I created a standard blazor application called BlazorApp and build it as a Library. I copied over all the files from the BlazorApp bin/ folder to a folder called plugins/BlazorApp/ in my HostApp bin/ folder.

The following HostApp program demonstrates my error:

       static void Main(string[] args)
       {
            var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
            var loaders = new List<PluginLoader>();
            foreach (var dir in Directory.GetDirectories(pluginsDir))
            {
                var dirName = Path.GetFileName(dir);
                var pluginDll = Path.Combine(dir, dirName + ".dll");
                if (File.Exists(pluginDll))
                {
                    var loader = PluginLoader.CreateFromAssemblyFile(
                        pluginDll,
                        sharedTypes:new Type[]{});
                    loaders.Add(loader);
                }
            }

            // Create an instance of plugin types
            foreach (var loader in loaders)
            {
                foreach (var pluginType in loader
                    .LoadDefaultAssembly()
                    .GetTypes()
                    .Where(t => t.Name.Equals("Program")))
                {

                    var program = pluginType.Assembly.DefinedTypes.FirstOrDefault(f => f.Name.Equals("Program"));
                    program.GetDeclaredMethod("Main")?.Invoke(null, new string[0]);
                }
            }
    }

So what am I missing here? When I build the BlazorApp as a regular executable, it runs fine, like it should do. When I try to inspect the executable using ILSpy, it gives me the same error when trying to open the referenced assembly like AspNetCore.Components, which isnt in the BlazorApp bin folder either. So somewhere the executable is able to find it whereas the plugin loader is not.

Due to restrictions I am not able to include AspNetCore in my regular .net5 project but figured as they're both targeting .net 5 as framework it should work, but apparently not.