gpt4 book ai didi

c# - 对于动态加载的 Razor 类库,ASP.NET Core 运行时编译失败并显示 "Cannot find compilation library location for package"

转载 作者:行者123 更新时间:2023-12-03 23:10:17 25 4
gpt4 key购买 nike

我正在使用以下代码将 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);
}

这工作正常, Controller 和 View 最初正在工作。但是如果我还添加了 Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation包和以下到 Startup.cs :
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(new PhysicalFileProvider(Path.Combine(WebHostEnvironment.ContentRootPath, "..\\AppPlugin")));
});

我一编辑 *.cshtml 就会收到以下异常-文件:

InvalidOperationException: Cannot find compilation library location for package 'AppPlugin'

  • Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List assemblies)
  • Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()

  • Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPartExtensions+<>c.b__0_0(CompilationLibrary library)

  • 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 中省略插件路径运行时编译适用于“非插件 View ”。

最佳答案

通过使用 razor 类库的以下项目设置,我能够使其工作。关键是将 PreserveCompilationContext 设置为 false。

<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<PreserveCompilationContext>false</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>

除此之外,您还必须添加对主插件程序集和 View 中使用的任何其他程序集的引用。
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(new PhysicalFileProvider(Path.Combine(WebHostEnvironment.ContentRootPath, "..\\AppPlugin")));
opts.AdditionalReferencePaths.Add(pluginAssembly.Location);
});

关于c# - 对于动态加载的 Razor 类库,ASP.NET Core 运行时编译失败并显示 "Cannot find compilation library location for package",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com