ASP.Net Core注册控制器在运行时 [英] ASP.Net Core register Controller at runtime

查看:257
本文介绍了ASP.Net Core注册控制器在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在问自己是否可以在运行时加载带有 Controller s的DLL并使用它。

I am asking myself if it is possible to load a DLL with Controllers in it at runtime and use it.

我发现的唯一解决方案是通过 StartUp 上的 ApplicationPart 添加一个程序集:

The only solution I've found is to add an assembly via ApplicationPart on the StartUp:

var builder = services.AddMvc();
builder.AddApplicationPart(
    AssemblyLoadContext.Default.LoadFromAssemblyPath(
        @"PATH\ExternalControllers.dll"
));

有人知道是否可以注册 Controller 在任何时候,因为该解决方案的问题是,当您想使用 Controller <添加另一个DLL时,必须重新启动 WebService 。 / code> s。只要您可以随时添加它们并随时在应用程序中注册它们,那就太好了。

Do anyone know if it is possible to register Controller at any time, because the issue with that solution is, that you have to restart the WebService when you want to add another DLL with Controllers in it. It would be nice when you just can add them at any time and register them at any time in the application.

推荐答案

现在可以在.net core 2.0+上实现

This is possible now on .net core 2.0+

请参见代码 ActionDescriptorCollectionProvider.cs

    public ActionDescriptorCollectionProvider(
        IEnumerable<IActionDescriptorProvider> actionDescriptorProviders,
        IEnumerable<IActionDescriptorChangeProvider> actionDescriptorChangeProviders)
    {
        _actionDescriptorProviders = actionDescriptorProviders
            .OrderBy(p => p.Order)
            .ToArray();

        _actionDescriptorChangeProviders = actionDescriptorChangeProviders.ToArray();

        ChangeToken.OnChange(
            GetCompositeChangeToken,
            UpdateCollection);
    }

步骤1:实施 IActionDescriptorChangeProvider 类:

public class MyActionDescriptorChangeProvider : IActionDescriptorChangeProvider
{
    public static MyActionDescriptorChangeProvider Instance { get; } = new MyActionDescriptorChangeProvider();

    public CancellationTokenSource TokenSource { get; private set; }

    public bool HasChanged { get; set; }

    public IChangeToken GetChangeToken()
    {
        TokenSource = new CancellationTokenSource();
        return new CancellationChangeToken(TokenSource.Token);
    }
}

步骤2:在Startup.ConfigureServices()上添加Singleton:

Step 2:AddSingleton on Startup.ConfigureServices():

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddSingleton<IActionDescriptorChangeProvider>(MyActionDescriptorChangeProvider.Instance);
    services.AddSingleton(MyActionDescriptorChangeProvider.Instance);
}

第3步:在运行时注册控制器:

Step 3: Register controller at runtime:

public class TestController : Controller
{
    private readonly ApplicationPartManager _partManager;
    private readonly IHostingEnvironment _hostingEnvironment;
    public TestController(
        ApplicationPartManager partManager,
        IHostingEnvironment env)
    {
        _partManager = partManager;
        _hostingEnvironment = env;
    }
    public IActionResult RegisterControllerAtRuntime()
    {
        string assemblyPath = @"PATH\ExternalControllers.dll";
        var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
        if (assembly != null)
        {
            _partManager.ApplicationParts.Add(new AssemblyPart(assembly));
            // Notify change
            MyActionDescriptorChangeProvider.Instance.HasChanged = true;
            MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel();
            return Content("1");
        }
        return Content("0");
    }
}

这篇关于ASP.Net Core注册控制器在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆