Razorlight: .net core 3.0 + asp.net web api - code compiled but run time Error came

Created on 2 Jun 2019  Â·  29Comments  Â·  Source: toddams/RazorLight

System.InvalidOperationException
HResult=0x80131509
Message=Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery
Source=Microsoft.Extensions.DependencyModel
StackTrace:
at Microsoft.Extensions.DependencyModel.Resolution.ReferenceAssemblyPathResolver.TryResolveAssemblyPaths(CompilationLibrary library, List1 assemblies) at Microsoft.Extensions.DependencyModel.Resolution.CompositeCompilationAssemblyResolver.TryResolveAssemblyPaths(CompilationLibrary library, List1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List1 assemblies) at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths() at System.Linq.Enumerable.SelectManySingleSelectorIterator2.MoveNext()
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
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.RoslynCompilationService.CompileAsync(IGeneratedRazorTemplate razorTemplate)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at RazorLight.Compilation.TemplateFactoryProvider.d__10.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at RazorLight.Compilation.TemplateFactoryProvider.d__8.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at RazorLight.RazorLightEngine.d__14.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at RazorLight.RazorLightEngine.d__11.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Micro.Svc.Ad.Write.CreateAds.Services.CreateAdsService.d__8.MoveNext() in C:\Users\chinn\source\repos\one\Micro.Svc.Ad.Write.CreateAds\Services\CreateAdsService.cs:line 86

Most helpful comment

Attention: @toddams @smirnovp88 @chinnarao

I finally have a working solution upgraded to netcoreapp3. The solution has 3 new projects as well as a sample Postman collection under the samples directory.

There are a number of compiler warning and references to obsolete methods. I have not tried to fix those. If this code is accepted as a possible solution by @toddams then I will continue to help with the conversion of the code.

RazorLight.zip

Please let me know what you think...

The one change I had to make in my consuming WebAPI project that might not be obvious from the source.
<PreserveCompilationReferences>true</PreserveCompilationReferences>

All 29 comments

I have same issue. Installed Microsoft.AspNetCore.Antiforgery package and get error _Cannot find reference assembly 'Microsoft.AspNetCore.Antiforgery.dll' file for package Microsoft.AspNetCore.Antiforgery.Reference_.
Any help appreciated. Thanks

Same here, I have tried to contact the developer. If someone can just let us know if this will be updated to handle .Net Core 3. I am willing to assist if I know this has a future and that someone else has not started on it already.

Attention: @toddams @smirnovp88 @chinnarao

I finally have a working solution upgraded to netcoreapp3. The solution has 3 new projects as well as a sample Postman collection under the samples directory.

There are a number of compiler warning and references to obsolete methods. I have not tried to fix those. If this code is accepted as a possible solution by @toddams then I will continue to help with the conversion of the code.

RazorLight.zip

Please let me know what you think...

The one change I had to make in my consuming WebAPI project that might not be obvious from the source.
<PreserveCompilationReferences>true</PreserveCompilationReferences>

@jacodv do you happen to have a fork w commits to actually see the differences more easily?

@christophwille, I have a branch that I created from the cloned master. I tried to create a pull request without any success.

You mean you cloned this repo? Well pushing wouldn't work unless you were a member of the team here. That's why I asked about your fork into your account.

Nope, sorry. I cloned it. If I can get what I have into a fork, then I am willing to do it.

Fork the repo into your own account (on GH), and then add a new remote origin locally using git. That then allows you to take the data from the clone to your fork.

Cool, sounds like a plan. I will get to it in the morning. Thank you for the pointer...

@christophwille: Excuse my ignorance. I have pushed to this branch on my fork (https://github.com/jacodv/RazorLight/tree/branch_jdevil-UpgradeToNetCore3)
I hope you can access it.

Yes, I can access it. To recap: updating the references + PreserveCompilationReferences is what did the trick in the end?

I see one issue here: 2.1 is the LTS, 2.2 goes out of support almost immediately when 3.0 ships. With those package changes it won't work for both 2.x and 3.0. (and PreserveCompilationReferences is ... strange)

fyi, I took a page from https://github.com/toddams/RazorLight/issues/248#issuecomment-499436254 and tried that myself at https://github.com/christophwille/myhaflinger.com/commit/2d1292fd2170078b2168f452ff3bbde0119c5f5e - those two lines make it work (note I had MvcRazorCompileOnPublish previously, so you'll have to try if that is necessary too)

@christophwille, just to be clear. You are using the current Nuget package 2.0.0-beta1 and you added the new project items and it worked?

Yes, the plain package from NuGet with those changes to the csproj.

Brilliant! Thank you.

I've created a RazorLight version for .NetCore 3.0 https://www.nuget.org/packages/RazorLight.NetCore3/ from https://github.com/whyseco/RazorLight

I experiment the same issue, has anyone solved this?

After some testing I found a solution.

I add the followinf line in the *.csproj and works with 2.0.-beta1 version

<PreserveCompilationReferences>true</PreserveCompilationReferences>

Hope help some one

@kYann a new RazorLight? Why?

This actualy repository is come to the die?

I've made good progress on my side - only 8 tests left to fix.

After some testing I found a solution.

I add the followinf line in the *.csproj and works with 2.0.-beta1 version

<PreserveCompilationReferences>true</PreserveCompilationReferences>

Hope help some one

@ssauriera still have the issue(2.0-beta1), I'm using RazorLight in a separate class library project. Which project file did you add this element, in class library project or in the web (api/mvc) project?

@christophwille, just to be clear. You are using the current Nuget package 2.0.0-beta1 and you added the new project items and it worked?

@jacodv @christophwille This doesn't work for me. I have .net core 3.0 and using RazorLight 2.0.0-beta1. I still get the issue defined in this thread.

My guess is that your executing assembly does not have the "PreserveCompilationReferences" attribute. In my case, I had to define it in the entry point (API) as well as the assembly that references the actual RazorLight NuGet package

On Fri, 6 Dec 2019 at 08:33, Chris Valencia <[email protected]notifications@github.com> wrote:

@christophwillehttps://github.com/christophwille, just to be clear. You are using the current Nuget package 2.0.0-beta1 and you added the new project items and it worked?

@jacodvhttps://github.com/jacodv @christophwillehttps://github.com/christophwille This doesn't work for me. I have .net core 3.0 and using RazorLight 2.0.0-beta1. I still get the issue defined in this thread.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/toddams/RazorLight/issues/258?email_source=notifications&email_token=ABMLO34CZAN3VRJEQ2NI4STQXHWZXA5CNFSM4HSCGSAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGDFHCY#issuecomment-562451339, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABMLO33PHISJXCSZZHNWWDTQXHWZXANCNFSM4HSCGSAA.

@jacodv I have it in my executing assembly which is the web api project.

My guess is that your executing assembly does not have the "PreserveCompilationReferences" attribute. In my case, I had to define it in the entry point (API) as well as the assembly that references the actual RazorLight NuGet package On Fri, 6 Dec 2019 at 08:33, Chris Valencia <[email protected]notifications@github.com> wrote: @christophwillehttps://github.com/christophwille, just to be clear. You are using the current Nuget package 2.0.0-beta1 and you added the new project items and it worked? @jacodvhttps://github.com/jacodv @christophwillehttps://github.com/christophwille This doesn't work for me. I have .net core 3.0 and using RazorLight 2.0.0-beta1. I still get the issue defined in this thread. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub<#258?email_source=notifications&email_token=ABMLO34CZAN3VRJEQ2NI4STQXHWZXA5CNFSM4HSCGSAKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGDFHCY#issuecomment-562451339>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABMLO33PHISJXCSZZHNWWDTQXHWZXANCNFSM4HSCGSAA.

@jacodv which root element of both referencing project and api project need to define it?

Code enters API, then business logic is injected which depends on razor code, this is also injected.

I have the following:
API
<PropertyGroup> <TargetFramework>netcoreapp3.0</TargetFramework> <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> <PreserveCompilationReferences>true</PreserveCompilationReferences> <LangVersion>preview</LangVersion> <ServerGarbageCollection>true</ServerGarbageCollection> <UserSecretsId>---</UserSecretsId> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath> </PropertyGroup>

Implementation/Business Logic
<PropertyGroup> <TargetFrameworks>netstandard2.1</TargetFrameworks> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <PreserveCompilationContext>true</PreserveCompilationContext> </PropertyGroup>

Razor Functionality
<PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> <PreserveCompilationContext>true</PreserveCompilationContext> </PropertyGroup>

Code enters API, then business logic is injected which depends on razor code, this is also injected.

I have the following:
API
<PropertyGroup> <TargetFramework>netcoreapp3.0</TargetFramework> <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks> <PreserveCompilationReferences>true</PreserveCompilationReferences> <LangVersion>preview</LangVersion> <ServerGarbageCollection>true</ServerGarbageCollection> <UserSecretsId>---</UserSecretsId> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerComposeProjectPath>..\docker-compose.dcproj</DockerComposeProjectPath> </PropertyGroup>

Implementation/Business Logic
<PropertyGroup> <TargetFrameworks>netstandard2.1</TargetFrameworks> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> <PreserveCompilationContext>true</PreserveCompilationContext> </PropertyGroup>

Razor Functionality
<PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> <PreserveCompilationContext>true</PreserveCompilationContext> </PropertyGroup>

Wow it really works! Thank you for staying active on my inquiries, God bless you!

Updated FAQ with this information.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nukec picture nukec  Â·  3Comments

AdrianPell picture AdrianPell  Â·  4Comments

Hubert-Rybak picture Hubert-Rybak  Â·  5Comments

danwalmsley picture danwalmsley  Â·  7Comments

diegobrum picture diegobrum  Â·  5Comments