toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.5k stars 259 forks source link

Executing via LinqPad throws "RazorLight.RazorLightException: Can't load metadata reference" #443

Closed raxuser100 closed 3 years ago

raxuser100 commented 3 years ago

I am using 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!

Attached a simple test linqpad file in the Zip. Also the source code is shown below, just save it as XXX.linq.

Is there any way of getting around this error?

razorlight_test.zip

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

Linq file code:

<Query Kind="Program">
  <NuGetReference Prerelease="true">RazorLight.Unofficial</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
jzabroski commented 3 years ago

Did you try asking Linqpad support? Joseph is very good about supporting people with paid licenses.

I also will note RazorLight.Unofficial isn't supported by this repo.