toddams / RazorLight

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

Error when running in AWS Lambda #259

Closed pbrianmackey closed 4 years ago

pbrianmackey commented 5 years ago

RazorLight 2.0.0-beta1

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.

We use circleci for deployments. The API that's having this problem is hosted in AWS Lambda. The issue only happens in AWS Lambda. On the local environment we have no problem.

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())

toddams commented 4 years ago

I will close all Lambda and Functions issues, as it we do not support these environments yet. There must be something specific about the environment that executes your functions

msorin commented 4 years ago

We were able to use Razorlight in a Lambda by supplying this setting "msbuild-parameters": "/p:PreserveCompilationContext=true" in the aws-lambda-tools-defaults.json file.