RazorLight Could not load file or assembly System.Threading.AccessControl

The goal is to generate HTML from a .cshtml file

When using RazorLight I get the following error:

 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)

I installed the NuGet package RazorLight 2.0.0-rc.3 and this is my code to generate html from a .cshtml file:

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);
}

Trying to add the System.Threading.AccessControl NuGet Package, but then another file or assembly can't be loaded (so never ending story of adding packages). There must be another solution.

I am using .NET Core 3.1.

4

1 Answer

For me the issue was that the Engine was used in a IRequestHandler from Mediatr.

Outsourcing the engine to the controller instead of having it inside the IRequestHandler fixed the issue.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like