稍后会在运行时期间添加或修改ASP.net Core Controller

时间:2018-10-23 18:14:31

标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-core

我有一个通用控制器,该控制器在启动时会针对自定义类型进行初始化。我想实现以后添加,刷新和删除这些控制器的功能。 (所有5分钟,通过API调用,...)这怎么可能?新类型由IExternalCompiler提供

public class ExternalControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
{
    private readonly IExternalCompiler _entityCompiler;

    public ExternalControllerFeatureProvider(IExternalCompiler entityCompiler)
    {
        _entityCompiler = entityCompiler;
    }

    public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
    {
        foreach (var candidate in _entityCompiler.GetTypes())
        {
            feature.Controllers.Add(
                typeof(GenericController<>).MakeGenericType(candidate).GetTypeInfo()
            );
        }
    }
}

此IApplicationFeatureProvider已添加到Startup中。配置到ApplicationPartManager

0 个答案:

没有答案