ASP.NET Core运行时编译失败,并显示“找不到包的编译库位置".用于动态加载的Razor类库 [英] ASP.NET Core Runtime Compilation fails with "Cannot find compilation library location for package" for dynamically loaded Razor Class Library

查看:297
本文介绍了ASP.NET Core运行时编译失败,并显示“找不到包的编译库位置".用于动态加载的Razor类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将Razor类库动态加载到我的 ASP.NET Core 3.0 应用程序中:

I am using the following code to dynamically load a Razor Class Library into my ASP.NET Core 3.0 app:

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 :

This works fine and controllers and views are initially working. But if I also add the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation package and the following to the Startup.cs:

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

我在编辑 *.cshtml 文件时得到以下异常:

I get the following exception as soon as I edit a *.cshtml-file:

InvalidOperationException:找不到编译库位置对于"AppPlugin"包

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

  • Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver解析器,列出程序集)
  • Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()

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

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

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

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths()

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetReferencePaths()

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()

是否还需要加载其他内容才能使其正常工作?
如果我从 FileProviders 中省略了插件路径,则运行时编译适用于"non-plugin-views".

Is there something else I need to load to get this to work?
If I omit the plugin-path from the FileProviders runtime-compilation works for "non-plugin-views".

推荐答案

使用剃刀类库的以下项目设置,可以使此工作正常进行.关键是将PreserveCompilationContext设置为false.

I was able to get this working by using the following project settings for the razor class library. The key piece was to set PreserveCompilationContext to 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>

除此之外,您还必须添加对主插件程序集以及视图中使用的所有其他程序集的引用.

In Addition to this, you'll have to add a reference to your main plugin assembly and any additional assemblies used in your views.

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

这篇关于ASP.NET Core运行时编译失败,并显示“找不到包的编译库位置".用于动态加载的Razor类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆