【发布时间】:2019-11-11 08:20:58
【问题描述】:
我正在使用以下代码将 Razor 类库动态加载到我的 ASP.NET Core 3.0 应用程序中:
var pluginAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
var partFactory = ApplicationPartFactory.GetApplicationPartFactory(pluginAssembly);
foreach (var part in partFactory.GetApplicationParts(pluginAssembly))
MvcBuilder.PartManager.ApplicationParts.Add(part);
var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(pluginAssembly, throwOnError: true);
foreach (var assembly in relatedAssemblies)
{
partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly);
foreach (var part in partFactory.GetApplicationParts(assembly))
MvcBuilder.PartManager.ApplicationParts.Add(part);
}
这可以正常工作,并且控制器和视图最初可以正常工作。但是,如果我还将Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 包和以下内容添加到Startup.cs:
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(new PhysicalFileProvider(Path.Combine(WebHostEnvironment.ContentRootPath, "..\\AppPlugin")));
});
编辑*.cshtml-file 后,我立即收到以下异常:
InvalidOperationException:找不到编译库位置 对于包'AppPlugin'
- Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver 解析器,列出程序集)
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPartExtensions+c.b__0_0(CompilationLibrary 图书馆)
- System.Linq.Enumerable+SelectManySingleSelectorIterator.MoveNext()
- System.Collections.Generic.List.InsertRange(int index, IEnumerable collection)
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths()
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()
是否需要加载其他内容才能使其正常工作?
如果我从FileProviders 中省略插件路径,则运行时编译适用于“非插件视图”。
【问题讨论】:
-
你有没有尝试过这样的事情:
services.AddControllersWithViews() .AddRazorRuntimeCompilation(options => options.FileProviders.Add( new PhysicalFileProvider(appDirectory)));? -
@Rena 感谢您的意见,但这并没有帮助:-(
标签: c# asp.net-mvc asp.net-core razor plugins