如何在运行时动态加载ASP.NET Core Razor视图

时间:2018-01-11 12:22:57

标签: razor asp.net-core

是否可以在运行时从单独的程序集引用ASP.NET Core Razor视图? 我知道如何使用IActionDescriptorChangeProvider动态加载控制器,但找不到视图的方法 我想创建一个简单的插件系统并管理插件而无需重启应用程序。

1 个答案:

答案 0 :(得分:3)

我正在创建一个动态且完全模块化(基于插件)的应用程序,在该应用程序中,用户可以在运行时将插件程序集拖放到文件监视的目录中,以添加控制器和已编译的视图。

我遇到了与您相同的问题。最初,即使我已经通过 ApplicationPartManager 服务正确添加了程序集,MVC仍未“检测”控制器和视图。

我已经解决了控制器问题,正如您所说,可以使用 IActionDescriptorChangeProvider 处理。

但是,对于观点问题,似乎没有内置类似的机制。我在Google上搜寻了几个小时,找到了您的信息(以及其他许多信息),但没有人回答。我差点放弃。差不多。

我开始对ASP.NET Core源进行爬网,并实现了我认为与查找编译视图有关的所有服务。晚上的大部分时间都花了我的头发,然后……EUREKA。

我发现负责提供那些已编译视图的服务是默认的IViewCompiler(又名DefaultViewCompiler),它又由IViewCompilerProvider(又名DefaultViewCompilerProvider)提供。

您实际上需要同时实现这两个功能,才能使其按预期工作。

IViewCompilerProvider:

 public class ModuleViewCompilerProvider
    : IViewCompilerProvider
{

    public ModuleViewCompilerProvider(ApplicationPartManager applicationPartManager, ILoggerFactory loggerFactory)
    {
        this.Compiler = new ModuleViewCompiler(applicationPartManager, loggerFactory);
    }

    protected IViewCompiler Compiler { get; }

    public IViewCompiler GetCompiler()
    {
        return this.Compiler;
    }

}

IViewCompiler:

public class ModuleViewCompiler
    : IViewCompiler
{

    public static ModuleViewCompiler Current;

    public ModuleViewCompiler(ApplicationPartManager applicationPartManager, ILoggerFactory loggerFactory)
    {
        this.ApplicationPartManager = applicationPartManager;
        this.Logger = loggerFactory.CreateLogger<ModuleViewCompiler>();
        this.CancellationTokenSources = new Dictionary<string, CancellationTokenSource>();
        this.NormalizedPathCache = new ConcurrentDictionary<string, string>(StringComparer.Ordinal);
        this.PopulateCompiledViews();
        ModuleViewCompiler.Current = this;
    }

    protected ApplicationPartManager ApplicationPartManager { get; }

    protected ILogger Logger { get; }

    protected Dictionary<string, CancellationTokenSource> CancellationTokenSources { get; }

    protected ConcurrentDictionary<string, string> NormalizedPathCache { get; }

    protected Dictionary<string, CompiledViewDescriptor> CompiledViews { get; private set; }

    public void LoadModuleCompiledViews(Assembly moduleAssembly)
    {
        if (moduleAssembly == null)
            throw new ArgumentNullException(nameof(moduleAssembly));
        CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        this.CancellationTokenSources.Add(moduleAssembly.FullName, cancellationTokenSource);
        ViewsFeature feature = new ViewsFeature();
        this.ApplicationPartManager.PopulateFeature(feature);
        foreach(CompiledViewDescriptor compiledView in feature.ViewDescriptors
            .Where(v => v.Type.Assembly == moduleAssembly))
        {
            if (!this.CompiledViews.ContainsKey(compiledView.RelativePath))
            {
                compiledView.ExpirationTokens = new List<IChangeToken>() { new CancellationChangeToken(cancellationTokenSource.Token) };
                this.CompiledViews.Add(compiledView.RelativePath, compiledView);
            }
        }
    }

    public void UnloadModuleCompiledViews(Assembly moduleAssembly)
    {
        if (moduleAssembly == null)
            throw new ArgumentNullException(nameof(moduleAssembly));
        foreach (KeyValuePair<string, CompiledViewDescriptor> entry in this.CompiledViews
            .Where(kvp => kvp.Value.Type.Assembly == moduleAssembly))
        {
            this.CompiledViews.Remove(entry.Key);
        }
        if (this.CancellationTokenSources.TryGetValue(moduleAssembly.FullName, out CancellationTokenSource cancellationTokenSource))
        {
            cancellationTokenSource.Cancel();
            this.CancellationTokenSources.Remove(moduleAssembly.FullName);
        }
    }

    private void PopulateCompiledViews()
    {
        ViewsFeature feature = new ViewsFeature();
        this.ApplicationPartManager.PopulateFeature(feature);
        this.CompiledViews = new Dictionary<string, CompiledViewDescriptor>(feature.ViewDescriptors.Count, StringComparer.OrdinalIgnoreCase);
        foreach (CompiledViewDescriptor compiledView in feature.ViewDescriptors)
        {
            if (this.CompiledViews.ContainsKey(compiledView.RelativePath))
                continue;
            this.CompiledViews.Add(compiledView.RelativePath, compiledView);
        };
    }

    public async Task<CompiledViewDescriptor> CompileAsync(string relativePath)
    {
        if (relativePath == null)
            throw new ArgumentNullException(nameof(relativePath));
        if (this.CompiledViews.TryGetValue(relativePath, out CompiledViewDescriptor cachedResult))
            return cachedResult;
        string normalizedPath = this.GetNormalizedPath(relativePath);
        if (this.CompiledViews.TryGetValue(normalizedPath, out cachedResult))
            return cachedResult;
        return await Task.FromResult(new CompiledViewDescriptor()
        {
            RelativePath = normalizedPath,
            ExpirationTokens = Array.Empty<IChangeToken>(),
        });
    }

    protected string GetNormalizedPath(string relativePath)
    {
        if (relativePath.Length == 0)
            return relativePath;
        if (!this.NormalizedPathCache.TryGetValue(relativePath, out var normalizedPath))
        {
            normalizedPath = this.NormalizePath(relativePath);
            this.NormalizedPathCache[relativePath] = normalizedPath;
        }
        return normalizedPath;
    }

    protected string NormalizePath(string path)
    {
        bool addLeadingSlash = path[0] != '\\' && path[0] != '/';
        bool transformSlashes = path.IndexOf('\\') != -1;
        if (!addLeadingSlash && !transformSlashes)
            return path;
        int length = path.Length;
        if (addLeadingSlash)
            length++;
        return string.Create(length, (path, addLeadingSlash), (span, tuple) =>
        {
            var (pathValue, addLeadingSlashValue) = tuple;
            int spanIndex = 0;
            if (addLeadingSlashValue)
                span[spanIndex++] = '/';
            foreach (var ch in pathValue)
            {
                span[spanIndex++] = ch == '\\' ? '/' : ch;
            }
        });
    }

}

现在,您需要找到现有的IViewCompilerProvider描述符,并将其替换为您自己的描述符,如下所示:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        ServiceDescriptor descriptor = services.FirstOrDefault(s => s.ServiceType == typeof(IViewCompilerProvider));
        services.Remove(descriptor);
        services.AddSingleton<IViewCompilerProvider, ModuleViewCompilerProvider>();
    }

然后,在加载编译的视图插件程序集后,只需进行以下调用:

ModuleViewCompiler.Current.LoadModuleCompiledViews(compiledViewsAssembly);

在卸载已编译的视图插件程序集时,进行该调用:

ModuleViewCompiler.Current.UnloadModuleCompiledViews(compiledViewsAssembly);

这将取消并摆脱我们与插件程序集加载的已编译视图关联的IChangeToken。 这非常重要,如果您打算在运行时加载,卸载然后重新加载特定的插件程序集,因为否则MVC会对其进行跟踪,可能会禁止您的AssemblyLoadContext的卸载,并且在编译时会引发错误由于模型类型不匹配(在时间T加载的部件z的模型x与在时间T + 1加载的部件z的模型x不同)

希望有帮助;)