question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't load metadata ... this time in Azure Functions

See original GitHub issue

Describe the bug I’m opening this because I’m experiencing the same behavior as #127 but this time in an Azure Functions app:

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

To Reproduce Try the following Azure Function (using Azure Functions v2):


        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var engine = new RazorLightEngineBuilder()
                .UseMemoryCachingProvider()
                .Build();

            var template = "Hello, @Model.Name";
            var model = new ViewModel { Name = "Adrian" };

            var result = await engine.CompileRenderAsync("templateKey", template, model);

            return new OkObjectResult(result);
        }

Information (please complete the following information):

  • OS: Windows 10
  • Platform .Net Core 2.1
  • RazorLight version 2.0-beta1

Additional context I have PreserveCompilationContext true in the functions csproj file … but I suspect that this is probably not what’s meant by the entry assembly. It looks as if there a number of infrastructure assemblies involved before my function ever gets called.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:5

github_iconTop GitHub Comments

10reactions
jonarmstrongcommented, Jul 23, 2019

Just want to confirm the above by @drhkocher as that worked for me too but on Azure App Service. I was previously getting the following error:

System.InvalidOperationException: Cannot find compilation library location for package ‘Microsoft.AspNet.WebApi.Client’

My setup uses RazorLight for emails, I have my “Mailer” class that builds the Razor templates in a separate library to my main application. This separate library uses CSHTML files as embedded resources.

In this library’s “csproj” file, I needed <PreserveCompilationContext>true</PreserveCompilationContext> - didn’t need any of the other settings in there that you might see mentioned. I had to use the same “unofficial” beta 1.3 with .SetOperatingAssembly(Assembly.GetExecutingAssembly()) on the RazorLightEngineBuilder.

The reason for the unofficial beta is the NullReferenceException thrown from RazorLight.Compilation.DefaultMetadataReferenceManager in Resolve when you call SetOperatingAssembly with certain assemblies (like the currently executing one). Don’t know where the source code is of the unofficial beta so take it with a grain of salt that it isn’t potentially malicious.

As for why this works - I have no freaking idea. My templates don’t reference WebApi and my main application (an ASP.NET Core 2.2 site) works fine as it is so… 🤷‍♀️

Anyway, that took me hours of trial and error to fix as it worked fine without it on my local machine. Hopefully it saves someone else a bunch of time!

I don’t believe this bug should be closed as it currently stands. The above issue as detailed still exists with SetOperatingAssembly(Assembly.GetExecutingAssembly()). Can we please get the fix which exists on the Unofficial packaged moved up to the Official package?

6reactions
drhkochercommented, Nov 13, 2018

Same Problem: .NET Standard 2.0, Azure Function 2.0 Since the entry assembly is coming from the Azure Functions runtime, I cannot control this setting there, so I need a workaround…

UPDATE: I made it work with the following workaround:

  1. I used .SetOperatingAssembly(currentAssembly) to use my own assembly (with PreserveCompilationContext = true) as a starting point. By setting the assembly here it does not use the entry assembly (which is part of Microsoft Azure).

  2. I had to switch from Beta1 to the “unofficial” beta1.3 because otherwise I got a NullPointerException.

Maybe that could be added to the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Triggering Durable Function throws Null Reference exception
Azure.WebJobs.Script.WebHost: Unable to load metadata for function 'OrchestrateTest'. Function 'OrchestrateTest (Orchestrator)' was aborted.
Read more >
Unable to generate Azure Functions extensions metadata file
Unable to generate Azure Functions extensions metadata file. 2> at ExtensionsMetadataGenerator.ExtensionsMetadataGenerator.Generate(String ...
Read more >
Troubleshoot Python errors in Azure Functions
In your text editor, open the METADATA file and check the Classifiers: section. If the section doesn't contain Python :: 3 , Python...
Read more >
Azure function fails but the reason is not defined in the log
Hello, I have a couple of issues with azure function (Python) My function extracts data from SQL DB and put it to the...
Read more >
Azure functions intermittent function loading issue
Hi, we have an Azure function app running two functions in python on a shared Linux app service plan. Both functions run based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found