1

Using the awesome LinqPad which references a ClassLib which in turn executes the RazorLight engine. Physical cshtml files live in a local folder. 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!

Also, just running RazorLight from within LinqPad without referencing a classLib throws the same error. So below I've replicated this. Have tried official and unofficial versions of RazorLight, all throw same error.

Is there any way of getting around this error?

View: hello_world.cshtml "Hello, @Model.Name. Welcome to RazorLight repository"

LinqPad: RazorLightTest.linq

<Query Kind="Program">
  <NuGetReference Version="2.0.0-beta9">RazorLight</NuGetReference>
  <Namespace>RazorLight</Namespace>
  <Namespace>System.Threading.Tasks</Namespace>
</Query>

void Main()
{
   RazorLightTest().Dump("RazorLight output");
}
public async Task<string> RazorLightTest()
{
   var engine = new RazorLightEngineBuilder()
   .UseFileSystemProject(@"D:\Temp")
   .UseMemoryCachingProvider()
   .Build();

   var model = new { Name = "John Doe" };
   return await engine.CompileRenderAsync("hello_world.cshtml", model);
}
// You can define other methods, fields, classes and namespaces here
Rax
  • 665
  • 8
  • 19
  • The following example which illustrates what you need to do to get standard Razor working in LINQPad might give you a clue or point you in the right direction: http://share.linqpad.net/v9qmhb.linq – Joe Albahari Apr 29 '21 at 02:06
  • Thanks so much @JoeAlbahari (especially for creating LinqPad!). Your example certainly provides an insight into Razor compilation. Can't find a way of linking this code into RazorLight, other than AddMetadataReferences(). https://github.com/toddams/RazorLight/issues/188#issuecomment-523418738 This did not work either. – Rax Apr 30 '21 at 12:27
  • Hey @JoeAlbahari, have a favour to ask. Could I "borrow" a LinqPad license? Have a developer who could fix this issue and release it back to the community. Perhaps a lot of people could then also use LinqPad and Razor as a code generation engine. – Rax May 07 '21 at 05:47
  • please send a message to customer support to discuss – Joe Albahari May 09 '21 at 07:13
  • Thanks so much @JoeAlbahari. I filled out the question form on https://www.linqpad.net/Feedback.aspx – Rax May 13 '21 at 10:29
  • @Rax what was the solution here. I'm having the same issue! – John Babb Jul 02 '21 at 13:39
  • Hey @JohnBabb. Unfortunately I was unable to resolve the issue. Was also awaiting the kind offer from Joe for a license to borrow. We could work on a fix together? – Rax Jul 05 '21 at 11:15

0 Answers0