Razor 运行时编译不适用于动态加载的程序集

Razor Runtime Compilation doesn't work with dynamically loaded assembly

我无法让 Razor Runtime Compilation 与动态加载的程序集一起工作。我的代码:

Startup.cs

var assembly = Assembly.LoadFrom(someModuleDllPath);
var viewsAssembly = Assembly.LoadFrom(someModuleViewsDllPath);

services.AddControllersWithViews()
    .AddApplicationPart(assembly)
    .AddApplicationPart(viewsAssembly)
    .AddRazorRuntimeCompilation();
            
services.Configure<MvcRazorRuntimeCompilationOptions>(options => { 
    options.FileProviders.Add(new EmbeddedFileProvider(assembly)); 
});

使用这种方法,我需要重建加载的程序集项目并重新启动主机才能看到变化。当我将最后两行替换为:

services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
    options.FileProviders.Add(new PhysicalFileProvider(loadedAssemblyProjectContentRootPath)); 
});

我收到错误:

Cannot find compilation library location for package '<name of loaded assembly>'.

MvcRazorExcludeRefAssembliesFromPublish 设置为 false 不会改变任何内容。当我添加对项目的显式引用时,带有 PhysicalFileProvider 的选项有效。

我正在使用 .NET 5,并且两个项目都有 Microsoft.NET.Sdk.Web 类型。

有什么方法可以让它发挥作用吗?

确保 PreserveCompilationContext 的密钥设置为 false

并且要使用当前项目下的路径,即相对路径,比如下面的代码:

services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
    options.FileProviders.Add(new PhysicalFileProvider(Path.Combine(WebHostEnvironment.ContentRootPath, "..\<name_of_loaded_assembly>")));
    options.AdditionalReferencePaths.Add(pluginAssembly.Location);
});