Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnauthorizedAccessException when running on Azure Web App service #143

Open
aquisio opened this issue Jun 29, 2024 · 4 comments
Open

UnauthorizedAccessException when running on Azure Web App service #143

aquisio opened this issue Jun 29, 2024 · 4 comments

Comments

@aquisio
Copy link

aquisio commented Jun 29, 2024

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
Copy link
Owner

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
Copy link
Author

aquisio commented Jul 1, 2024

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
Copy link
Author

aquisio commented Jul 4, 2024

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants