1

This problem is specific to RazorLight.

Error:

Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file

This error pops up only after we deploy to AWS. On the local machine things work fine. I've already added PreserveCompilationContext to the *.csproj file.

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

We use circleci for deployments. The API that's having this problem is hosted in AWS Lambda.

private async Task<string> GenerateText(string template, ProseModel model)
        {
            var engine = new RazorLightEngineBuilder()
              .UseMemoryCachingProvider()
              .Build();

            try
            {
                // ERROR thrown on next line
                var result = await engine.CompileRenderAsync(Guid.NewGuid().ToString(), template, model);
                return result;
            }
            catch(Exception e)
            {
                Logger.LogError("Error generating template", e);
                throw e;
            }
        }

I found that people are getting this same error in Azure Functions. Is that similar to Lambda's and maybe requires a similar fix? If yes, how can I fix this in a Lambda?

I've also tried to set SetOperatingAssembly(Assembly. GetExecutingAssembly())

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

1 Answers1

1

I ran into the same issue but the fix that you posted for the Azure Function hack worked for me. You must make sure to replace the "RazorLight" package with the "RazorLight.Unofficial" package version beta1.3. Then it should work.

The problem is that the entry assembly when running on Lambda is called:

Bootstrap, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

Which I'm assuming isn't compiled to preserve the compilation context.

ChrisAndy
  • 296
  • 3
  • 2