RazorLight: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery

I use netcore 3.1 . when I use following code ,i got error.

var path = Path.Combine(_hostingEnvironment.ContentRootPath, "Views");
           var engine = new RazorLightEngineBuilder() 
               .UseFileSystemProject(path)
              .UseMemoryCachingProvider()
              .Build();

           var result = await engine.CompileRenderStringAsync("test", @"@using RazorLightTest.Controllers 
@model Test
Hello @Model.Name", new Test { Name = "World" });
System.InvalidOperationException: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery
   at Microsoft.Extensions.DependencyModel.Resolution.ReferenceAssemblyPathResolver.TryResolveAssemblyPaths(CompilationLibrary library, List`1 assemblies)
   at Microsoft.Extensions.DependencyModel.Resolution.CompositeCompilationAssemblyResolver.TryResolveAssemblyPaths(CompilationLibrary library, List`1 assemblies)
   at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List`1 assemblies)
   at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
   at RazorLight.Compilation.DefaultMetadataReferenceManager.<>c.<Resolve>b__10_0(CompilationLibrary library)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(Assembly assembly, DependencyContext dependencyContext)

When i include the package “Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation”.The problem is solved.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 24 (3 by maintainers)

Most upvoted comments

Got the same error with netcore 3.0, adding this in my .csproj fixed the issue

<PropertyGroup>
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
    <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

Ref: https://github.com/aspnet/AspNetCore/issues/14418#issuecomment-535107767

<PropertyGroup>
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
    <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

@Wingjam I confirm it does, kudos!

Yes, this area: https://github.com/toddams/RazorLight#im-getting-cant-load-metadata-reference-from-the-entry-assembly-exception

I think it would make sense to add a separate FAQ for this and note this happens on .NET Core App 3.0 and higher, and link to the ASP.NET Core link https://github.com/aspnet/AspNetCore/issues/14418#issuecomment-535107767 - The key finding here is that the .NET SDK Tools team decided to introduce a new compilation directive for managing library dependencies. Don’t worry about perfect wording - let’s make it easily discoverable first for others to reduce Issues churn here. If people are confused, we can refine the language.

I confirm that adding the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget to my assembly seems to fix the issue here.

I’m using paket and my paket reference file looks like this:

RazorLight
Microsoft.AspNetCore.Mvc.Razor
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation

Once repro is complete, may be worth looking into packaging Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation - if that is even the real issue here.