RazorLight: RazorLightException: Cant load metadata... PreserveCompilationContext

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.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 30 (2 by maintainers)

Most upvoted comments

  1. Go to project’s folder location
  2. Write “.csproj” then search
  3. Find related csproj file and open it via text editor
  4. Find <PropertyGroup> node.
  5. Add <PreserveCompilationContext>true</PreserveCompilationContext> inside it.
  6. Rebuild the solution.

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.

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.

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?

@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.

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?

It worked for me in .net 8

@AlexTeixeira @b1tburn3r I was able to get it to work by cleaning the project and rebuilding.

So the following worked

dotnet clean
dotnet test