Assambly.GetTypes() выдает исключение при добавлении в плагин пакетов Asp.NetCore.Mvc

Решил реализовать плагинную систему с использованием ASP .NET Core MVC. Хост приложение представляет из себя .NET Core WPF шаблон, но также в нем запускается WebHost. Плагин представляет из себя билиотеку классов .NET Core .dll. И все замечательно работало, пока я не добавил MVC. В хосте MVC работает, а в плагине успел только добавить Asp.NetCore.Mvc NuGet и все сломалось. Assambly.GetTypes() выдает исключение "Unable to load one or more of the requested types". Удаляю NuGet, опять все работает. Microsoft писали

"Currently, plugins can't introduce new frameworks into the process. For example, you can't load a plugin that uses the Microsoft.AspNetCore.App framework into an application that only uses the root Microsoft.NETCore.App framework. The host application must declare references to all frameworks needed by plugins."

Но у меня плагин не добавляет новых пакетов в процесс хоста. Не пойму что еще не так. Вот код класса, который добавляет плагины(там половина кода взята с сайта Microsoft):

nternal static class PluginManager
{
    public static Dictionary<int, IPlugin> LoadedPlugins = new Dictionary<int, IPlugin>();

    static int id = 0;

    static string pluginDirectory = Path.Combine(AppContext.BaseDirectory, "Plugins\\netcoreapp3.1");
    static List<string> pluginPaths = new List<string>();

    static PluginManager()
    {
        LoadPlugins();
    }

    static void LoadPlugins()
    {
        if (Directory.Exists(pluginDirectory))
        {
            pluginPaths.AddRange(Directory.GetFiles(pluginDirectory + "\\", "*.dll"));
        }

        foreach (var path in pluginPaths)
        {
            Assembly assembly = LoadPlugin(path); //тут исключение, и кроме названия сборки ничего не грузится
            foreach (Type type in assembly.GetTypes())
            {
                if (Activator.CreateInstance(type) is IPlugin plugin)
                {
                    plugin.Id = id++;
                    LoadedPlugins.Add(plugin.Id, plugin);
                }
            }
        }
    }

    static Assembly LoadPlugin(string path)
    {
        PluginLoadContext context = new PluginLoadContext(path);
        return context.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(path)));
    }

    class PluginLoadContext : AssemblyLoadContext
    {
        private AssemblyDependencyResolver _resolver;

        public PluginLoadContext(string pluginPath)
        {
            _resolver = new AssemblyDependencyResolver(pluginPath);
        }

        protected override Assembly Load(AssemblyName assemblyName)
        {
            string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
            if (assemblyPath != null)
            {
                return LoadFromAssemblyPath(assemblyPath);
            }

            return null;
        }

        protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
        {
            string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
            if (libraryPath != null)
            {
                return LoadUnmanagedDllFromPath(libraryPath);
            }

            return IntPtr.Zero;
        }
    }
}

Ответы (1 шт):

Автор решения: Saint

Все. Разобрался. Это видимо ругался IIS-сервер В методе public void ConfigureServices(IServiceCollection services) класса плагина сделал вот так и все заработало:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().AddApplicationPart(typeof(MyPlugin).GetTypeInfo().Assembly).AddControllersAsServices();
    }

Я вообще готовое решение с плагинами нашел https://stackoverflow.com/questions/52722484/asp-net-core-mvc-2-1-mvc-views-in-plugin

→ Ссылка