question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

23reactions
Wingjamcommented, Dec 16, 2019

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

9reactions
nblx2k4commented, Apr 21, 2020
<PropertyGroup>
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
    <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

@Wingjam I confirm it does, kudos!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Cannot find reference assembly 'Microsoft.AspNetCore ...
1 Answer 1 ... Make sure you run command Ex. "dotnet publish -f netcoreapp2.1 -c Release" so you get a folder (bin\publish) which...
Read more >
Microsoft.AspNetCore.Antiforgery.dll - code.fitness
Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery. Apr 8, 2020. It happened again.
Read more >
no source - Cannot find reference assembly 'Microsoft. ...
Working fine locally with source code. Error: 4.60 - no source - Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' fail: Microsoft.
Read more >
Why am I getting 'Unable to find package Microsoft. ...
I was working just fine yesterday but this morning I opened my project and began getting 'Unable to find package Microsoft.AspNetCore.
Read more >
[Solved]-Copy Views folder for Asp.Net in .Net5-.net-core
InvalidOperationException: Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found