I'm getting the exception mentioned here:
https://github.com/toddams/RazorLight/issues/44
RazorLight.RazorLightException: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file
But I opened this because none of the suggestions resolved it. Clean/Rebuild wont fix, and adding <PreserveCompilationContext>true</PreserveCompilationContext> wont fix. I am using a Core 2.0 Unit Test app, but that shouldn't be any different than a simple console app.
Where did you add <PreserveCompilationContext>true</PreserveCompilationContext>?
Try putting it in "PropertyGroup".
Like this
<PropertyGroup>
...
<PreserveCompilationContext>true</PreserveCompilationContext>
...
</PropertyGroup>
I looked at the code in README and put it in ItemGroup, but it was not resolved.
I put it in PropertyGroup and I found it works.
Did it solve the issue? Waiting for feedback
Interesting. Yes that did fix it. Mine was in a PropertyGroup to begin with, but appended below other items listed in that group. I pulled it out into it's own PropertyGroup tag and it works. Cool, thanks.
So is this a hack that will always be necessary, or is there some more official fix that can be made so as not to require this manual edit?
You have to always set "PreserveCompilationContext". It was a mistake in documentation that it must be located in ItemGroup, and it's fixed now (thanks for the PR :)) Answering your question, it's not a hack but a requirement, as without this option RazorLight would not be able to compile your templates at runtime
I am trying to do this in an MSTest project for some light integration testing. As instructed, I put the PreserveCompilationContext element (with it set to true) in a property group tag (I used the existing property group tag at the top of the csproj file), however; it still gives me the error. I have run into issues similar to this in the past with unit tests. Is this something that does not work inside a unit test project and if you want to integration test compiling a razor file, you would need to test it in a console app like in the samples?
update: Just a quick sanity check for myself to ensure my code calling into RazorLight is set up properly and works as expected...
I tried pulling my DLL into a console application to confirm that it is working, (it works in the console app), this appears to be an issue with trying to run this in a unit test project.
Read my post above. You need to put it in its own isolated PropertyGroup. My unit test then worked fine.
I have the same issue as @b1tburn3r.
I add PreserveCompilationContext to true in an isolated PropertyGroup ( at the end of the file ), but my test in failing with the error :
Can't load metadata reference from the entry assembly
Can you provide a sample of working code with UnitTest ?
@AlexTeixeira @b1tburn3r I was able to get it to work by cleaning the project and rebuilding.
So the following worked
dotnet clean
dotnet test
<PropertyGroup> node.<PreserveCompilationContext>true</PreserveCompilationContext> inside it.i did the suggestions in this thread but it's still happening on net core 3.1. Any idea?
@xxeasterxx I just found this thread and it worked for me for a worker service.exe. Be sure you put the PropertyGroup in the csproj of your executable.
@billrob an .exe is not a .net core executable, though. @xxeasterxx issue is with netcoreapp3.1 target framework moniker.
@xxeasterxx Sorry I missed your response, but it's generally a good idea to open a new issue rather than reply to a closed one if you're stuck. You can leave a comment here and link back to the new issue to form breadcrumbs for others to follow as a courtesy. Additionally, "it's still happening" needs more information. Thank you!
@jzabroski My intentions was to ensure it wasn't on an underlying project and added to the project that contains the entry point to the application. I had made that mistake previously.
@billrob Great point. I had a coworker even who did that incorrectly.
I am using LinqPad which references ClassLib which in turn executes the RazorLight engine. I run into the same error:
RazorLight.RazorLightException: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file
I tried adding <PreserveCompilationContext> into the classlib csproj, though I know it will not work as it's not the entry point. LinqPad is the entry point!
Is there any way of getting around this error?
@raxuser100 From your description, you're using RazorLight.Unofficial, which isn't supported by this repository and may be quite old. nuget.org says that package was last updated last updated 6/30/2018 . Probably time for you to re-think your dependency.
You are right, thanks for pointing out version issue. Would this be the correct latest version to try:
or back in 2017 I note the official release:
Both throw the same error in LinqPad.
I've raised a StockOverflow question
https://stackoverflow.com/questions/67303505/linqpad-razorlight-executing-from-linqpad-throws-razorlight-razorlightexcepti
Thanks for your help,
Rakesh
From: John Zabroski @.>
Sent: 28 April 2021 16:33
To: toddams/RazorLight *@.>
Cc: raxuser100 @.>; Mention @.*>
Subject: Re: [toddams/RazorLight] RazorLightException: Cant load metadata... PreserveCompilationContext (#127)
@raxuser100https://github.com/raxuser100 From your description, you're using RazorLight.Unofficial, which isn't supported by this repository and may be quite old. nuget.org says that package was last updated last updated 6/30/2018 . Probably time for you to re-think your dependency.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/toddams/RazorLight/issues/127#issuecomment-828554116, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEK5YCAA2PTB4TFEFVAJKT3TLATGFANCNFSM4EJO5CZQ.
Please try the beta. I was hoping to do an official version soon but I got delayed by getting the covid-19.
So sorry to hear about your challenges with Covid... I do hope you are well and recovered now.
Thanks, I tried the
Is there some way of utilising MetadataReference to solve this problem? Perhaps a long shot!
From: John Zabroski @.>
Sent: 28 April 2021 17:31
To: toddams/RazorLight *@.>
Cc: raxuser100 @.>; Mention @.*>
Subject: Re: [toddams/RazorLight] RazorLightException: Cant load metadata... PreserveCompilationContext (#127)
Please try the beta. I was hoping to do an official version soon but I got delayed by getting the covid-19.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/toddams/RazorLight/issues/127#issuecomment-828597497, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEK5YCG3IQMWHCVQBMNFHKTTLAZ5FANCNFSM4EJO5CZQ.
Most helpful comment
I am trying to do this in an MSTest project for some light integration testing. As instructed, I put the PreserveCompilationContext element (with it set to true) in a property group tag (I used the existing property group tag at the top of the csproj file), however; it still gives me the error. I have run into issues similar to this in the past with unit tests. Is this something that does not work inside a unit test project and if you want to integration test compiling a razor file, you would need to test it in a console app like in the samples?
update: Just a quick sanity check for myself to ensure my code calling into RazorLight is set up properly and works as expected...
I tried pulling my DLL into a console application to confirm that it is working, (it works in the console app), this appears to be an issue with trying to run this in a unit test project.