目标是从 .cshtml 文件生成 HTML
使用 RazorLight 时出现以下错误:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Threading.AccessControl, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
File name: 'System.Threading.AccessControl, Version=4.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext)
at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
at RazorLight.Compilation.DefaultMetadataReferenceManager.GetReferencedAssemblies(Assembly a, IEnumerable`1 excludedAssemblies, HashSet`1 visitedAssemblies)+MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.GetReferencedAssemblies(Assembly a, IEnumerable`1 excludedAssemblies, HashSet`1 visitedAssemblies)+MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.GetReferencedAssemblies(Assembly a, IEnumerable`1 excludedAssemblies, HashSet`1 visitedAssemblies)+MoveNext()
at System.Linq.Set`1.UnionWith(IEnumerable`1 other)
at System.Linq.Enumerable.UnionIterator`1.FillSet()
at System.Linq.Enumerable.UnionIterator`1.ToArray()
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly, DependencyContext dependencyContext)
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly)
at RazorLight.Compilation.RoslynCompilationService.EnsureOptions()
at RazorLight.Compilation.RoslynCompilationService.get_ParseOptions()
at RazorLight.Compilation.RoslynCompilationService.CreateSyntaxTree(SourceText sourceText)
at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName)
at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
at RazorLight.Compilation.RazorTemplateCompiler.CompileAndEmit(RazorLightProjectItem projectItem)
at RazorLight.Compilation.RazorTemplateCompiler.OnCacheMissAsync(String templateKey)
我安装了 NuGet 包 RazorLight 2.0.0-rc.3 这是我从 .cshtml 文件生成 html 的代码:
private async Task<string> RenderRazorTemplate(string key, Type viewModelType, object viewModel)
{
var engine = new RazorLightEngineBuilder()
.UseEmbeddedResourcesProject(Assembly.GetExecutingAssembly()) // Use the Executing Assembly as project that embeds the .cshtml templates
.SetOperatingAssembly(viewModelType.Assembly) // Without this, you'll get a Exception saying that the Assembly can't find the AntiForgery.dll
.UseMemoryCachingProvider()
.Build();
return await engine.CompileRenderAsync(key, viewModel);
}
尝试添加 System.Threading.AccessControl NuGet 包,但无法加载另一个文件或程序集(因此添加包的故事永无止境)。一定有其他解决方案。
我正在使用.NET Core 3.1。
对我来说,问题是引擎被用在
IRequestHandler
的 Mediatr
中。
将引擎外包给控制器,而不是将其放在内部
IRequestHandler
解决了问题。
我只需通过 nuget 安装所有卸载程序集...它对我有用。