ASP.NET Core运行时编译失败,并显示“找不到包的编译库位置".用于动态加载的Razor类库

编程入门 行业动态 更新时间:2024-10-24 22:24:31
本文介绍了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类库

本文发布于:2023-11-16 13:00:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   类库   加载   位置   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!