RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

Azure Function v2 related issues

Open mrcarl79 opened this issue 7 years ago • 10 comments

Hi,

I'm trying to use RazorLight in an Azure Function v2, but I get a "Could not find or load a specific file" when it tries to compile the view.

I can't work out where this issue lies and wondered if it had anything to do with this:

When RazorLight compiles your template - it loads all the assemblies from your entry assembly and creates MetadataReference from it. This is a default strategy and it works in 99% of the time. But sometimes compilation crashes with an exception message like "Can not find assembly My.Super.Assembly2000". In order to solve this problem you can pass additional metadata references to RazorLight.

Any help would be appreciated :)

Thanks

Error

[19/03/2018 1:51:18 AM] A ScriptHost error has occurred

[19/03/2018 1:51:18 AM] Exception while executing function: Maintenance. RazorLight: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

[19/03/2018 1:51:18 AM] Exception while executing function: Maintenance

[19/03/2018 1:51:18 AM] Exception while executing function: Maintenance. RazorLight: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

[19/03/2018 1:51:18 AM] Function completed (Failure, Id=eafaae85-9698-4b0f-ada8-13061e0837f7, Duration=2409ms)

[19/03/2018 1:51:18 AM]

[19/03/2018 1:51:18 AM] Executed 'Maintenance' (Failed, Id=eafaae85-9698-4b0f-ada8-13061e0837f7)

[19/03/2018 1:51:18 AM] System.Private.CoreLib: Exception while executing function: Maintenance. RazorLight: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

[19/03/2018 1:51:18 AM]   Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is 'eafaae85-9698-4b0f-ada8-13061e0837f7'

[19/03/2018 1:51:18 AM] System.Private.CoreLib: Exception while executing function: Maintenance. RazorLight: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.DependencyModel, Version=2.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

Code

var path = $"{ context.FunctionAppDirectory }/Templates";
var engine = new RazorLightEngineBuilder()
   .UseFilesystemProject(path)
   .UseMemoryCachingProvider()
   .Build();
string result = await engine.CompileRenderAsync("MonthlyReport.cshtml", new MonthlyReport { DateRun = DateTime.Now });

View

@using` RazorLight
@using Models
@inherits TemplatePage<MonthlyReport>
<html>
    Date run: @Model.DateRun
</html>

Model

using System;
using System.Collections.Generic;
using System.Text;

namespace Models
{
    public class MonthlyReport
    {
        public DateTime DateRun { get; set; }
    }
}

mrcarl79 avatar Mar 19 '18 02:03 mrcarl79

So got some more input, as got past the first error after much research.

It seems the Azure Functions V2 CLI already has a lot of assemblies that come with it, one of which is that Microsoft.Extensions.DependencyModel but they include the version 2.0.0 and the issue is when this builds and runs they use their version 2.0.0 and your assembly wants 2.0.3 so it crashes.

After lots of research this is a common issue occurring with Azure Functions V2 CLI and nuget packages with different dependencies.

I did manage a hack of your razorlight.nuspec and changed the version your package requires to the following:

<dependency id="Microsoft.Extensions.DependencyModel" version="2.0.0" exclude="Build,Analyzers" />

Obviously this is not a satisfactory fix, but more proof it was a Azure Functions V2 CLI issue which I believe they are going to resolve.

However fixing that has now given the following error:

[19/03/2018 2:54:46 AM] A ScriptHost error has occurred
[19/03/2018 2:54:46 AM] Exception while executing function: Maintenance. RazorLight: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file.
[19/03/2018 2:54:46 AM] Exception while executing function: Maintenance
[19/03/2018 2:54:46 AM] Exception while executing function: Maintenance. RazorLight: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file.
[19/03/2018 2:54:46 AM] Function completed (Failure, Id=4e03d172-7a75-4aee-add9-bcdb2d9188a8, Duration=2515ms)
[19/03/2018 2:54:46 AM]
[19/03/2018 2:54:46 AM] Executed 'Maintenance' (Failed, Id=4e03d172-7a75-4aee-add9-bcdb2d9188a8)
[19/03/2018 2:54:46 AM] System.Private.CoreLib: Exception while executing function: Maintenance. RazorLight: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file.
[19/03/2018 2:54:46 AM]   Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '4e03d172-7a75-4aee-add9-bcdb2d9188a8'
[19/03/2018 2:54:46 AM] System.Private.CoreLib: Exception while executing function: Maintenance. RazorLight: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file.

Which looked like an easy fix and I added the following "<PreserveCompilationContext>true</PreserveCompilationContext>" to the .csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.6" />
    <PackageReference Include="RazorLight" Version="2.0.0-beta1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
    <None Update="Test.cshtml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <PropertyGroup>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>
</Project>

But it does not seem to recognise that property as the error persists, even after cleaning and rebuilding the project etc.

Have you any experience getting this to work with a Azure Functions V2, or is this something a bit ambitious to try and get working?

Thanks :)

mrcarl79 avatar Mar 19 '18 03:03 mrcarl79

Just as an update I tried switching to Azure Functions V1 and using RazorLight but still started encountering issues, but since that I have found DotLiquid a template engine which may be more suited to my requirements (sending emails) and works fine with Azure Functions V2 so my issue is no longer a problem for me. However I'm still happy to try and resolve this on a test project if it will help others, or close this issue if you wish.

mrcarl79 avatar Mar 20 '18 21:03 mrcarl79

I have the same problem. Functions app doesn't work. Console app works fine. Both calling the same library.

asoet avatar Apr 17 '18 00:04 asoet

Try to set OperatingAssembly to Assembly.GetCallingAssembly()

toddams avatar Jun 10 '18 10:06 toddams

This results in a different error: Same as #196

System.NullReferenceException: Object reference not set to an instance of an object.
at RazorLight.Compilation.DefaultMetadataReferenceManager.Resolve(DependencyContext dependencyContext)
at RazorLight.Compilation.RoslynCompilationService.EnsureOptions()
at RazorLight.Compilation.RoslynCompilationService.get_ParseOptions()
at RazorLight.Compilation.RoslynCompilationService.CreateSyntaxTree(SourceText sourceText)
at RazorLight.Compilation.RoslynCompilationService.CreateCompilation(String compilationContent, String assemblyName)
at RazorLight.Compilation.RoslynCompilationService.CompileAndEmit(IGeneratedRazorTemplate razorTemplate)
 at RazorLight.Compilation.RoslynCompilationService.CompileAsync(IGeneratedRazorTemplate razorTemplate)
--- End of stack trace from previous location where exception was thrown ---
   at RazorLight.Compilation.TemplateFactoryProvider.CompileAsync(IGeneratedRazorTemplate razorTemplate)
at RazorLight.Compilation.TemplateFactoryProvider.CreateFactoryAsync(String templateKey)
at RazorLight.RazorLightEngine.CompileTemplateAsync(String key)
at RazorLight.RazorLightEngine.CompileRenderAsync(String key, Object model, Type modelType, ExpandoObject viewBag)

asoet avatar Jun 20 '18 20:06 asoet

I have the same problem and the same error as above, after trying the suggested fix of setting OperatingAssembly. I cannot find a single independent Razor implementation that works on Azure Functions 2.x :(

zmarty avatar Nov 21 '18 05:11 zmarty

Same issue here. Sad times. Are there any updates to this?

... [08/03/2019 22:49:47] Executed 'Render' (Failed, Id=cbf33eb5-bef7-4d1e-8133-b4a1504b55a5) [08/03/2019 22:49:47] System.Private.CoreLib: Exception while executing function: Render. RazorLight: Can't load metadata reference from the entry assembly. Make sure PreserveCompilationContext is set to true in *.csproj file. [08/03/2019 22:49:47] Executed HTTP request: { ...

ThatBlokeCalledJay avatar Mar 08 '19 22:03 ThatBlokeCalledJay

Any updates to getting this to work in azure functions?

ilushka85 avatar Mar 06 '20 03:03 ilushka85

@ilushka85 Are you running into this on Azure Function v2 or v3?

jzabroski avatar Apr 18 '20 16:04 jzabroski

@ilushka85 Please also see #306 (read the thread)

jzabroski avatar Apr 18 '20 16:04 jzabroski