adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
561 stars 83 forks source link

UnauthorizedAccessException when running on Azure Web App service #143

Open aquisio opened 1 week ago

aquisio commented 1 week ago

My implementation of RazorEngineCore works fine in my local Windows development environment, but throws UnauthorizedAccessException when deployed as part of an Azure Web App (on a Windows instance).

As the following exception message indicates, the issue arises during template compile:

System.UnauthorizedAccessException: Access is denied. (0x80070005 (E_ACCESSDENIED))
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(IntPtr ptrNativeAssemblyBinder, IntPtr ptrAssemblyArray, Int32 iAssemblyArrayLen, IntPtr ptrSymbols, Int32 iSymbolArrayLen, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.InternalLoad(ReadOnlySpan`1 arrAssembly, ReadOnlySpan`1 arrSymbols)
   at System.Reflection.Assembly.Load(Byte[] rawAssembly, Byte[] rawSymbolStore)
   at RazorEngineCore.RazorEngineCompiledTemplate`1..ctor(RazorEngineCompiledTemplateMeta meta)

Strangely, some templates work perfectly, while others throw the above exception, with seemingly no significant difference between their template structure or content.

I've tried multiple different ways of supplying the templates to RazorEngineCore, including embedded resource files and hard-coded strings, but all approaches suffer the same exceptions.

Interestingly, the problem only occurs when the RazorEngineCore implementation and the templates are defined in a class library. If I move these elements into the root ASP.NET Core Web API project, RazorEngineCore renders reliably.

If you have any suggestions, I would appreciate them. Many thanks.

adoconnection commented 1 week ago

Hi, whats the NET version you use?

The exception happens here so the actual problem I believe in compiling template and its references.

My first guess is to go ang check whether there are any tricky dependencies of a class library (if any) Also I would have tried creating empty project with only one hello world template so see if its a generic RazorEngine problem or due to specific use case / environment.

aquisio commented 1 week ago

Hi Alexander.

Thanks very much for your helpful reply.

We are using .NET 8.0.

The failing templates do not reference any assemblies, although they do subclass RazorEngineTemplateBase, in order to implement your recommended pattern for accessing partial templates. As mentioned above, I have seen this pattern working correctly, when implemented in the ASP.NET Core root Web API project, but the same code fails when it exists in a class library.

As you suggested, I will create a the simplest possible demo of the issue and post here again when this is ready.

Thanks again.

aquisio commented 6 days ago

In case anyone else faces this issue, here is the solution that worked for me. The problem was entirely caused by using a low-tier Azure instance. When I upgraded to a higher tier (B1 or above) the issue disappeared.

adoconnection commented 5 days ago

Tim, thank you very much for feedback! I cant imagine how is this even possible, the only viable difference I see between tiers in terms of running NET app is memory. But then its OutOfMemoryException, not access issue. Very strange and interesting.