'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib' error when trying to parse template with Razor Engine
Asked Answered
L

2

7

I have to create a Asp.Net Web Api that is capable of sending emails. I managed in sending the email but only with a simple template stored in a variable, locally. The next step was to render a template from external file, like this:

            string filePath = @"C:\Data\EmailClient\EmailClient\EmailClient\EmailTemplate\ReceiptTemplate.cshtml";
            var config = new TemplateServiceConfiguration
                             {
                                 TemplateManager = new ResolvePathTemplateManager(new[] { "EmailTemplates" }),
                                 DisableTempFileLocking = true
                             };
            Engine.Razor = RazorEngineService.Create(config);

            if (File.Exists(filePath))
            {
                emailHtmlBody = Engine.Razor.RunCompile(filePath, null, email);
                mail.Body = emailHtmlBody;
            }

The problem is that, when the razor engine tries to parse the template, the following error appears:

"Could not load type 'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."

I was trying to parse the template from a string, and I am blocked with the same error, also.

Lacedaemon answered 19/11, 2018 at 15:5 Comment(3)
Have you tried adding a using statement at the top of your View? using System.Security or using System.Security.AccountManagementPalmira
I added what you suggested, nothing happened. The same error appears.Lacedaemon
I solved the problem by creating a Asp.Net core Web Api that targets a .Net Framework 4.6.1.Lacedaemon
B
4

You may see

<PackageReference Include="RazorEngine" Version="3.10.0" />  

or similar in your .csproj file, when what you really need is

<PackageReference Include="RazorEngine.Netcore" Version="3.1.0" />

so,

$ dotnet remove package RazorEngine
$ dotnet add package RazorEngine.Netcore --version 3.1.0

(or similar as the version updates) looks as though it's the answer
[Matt's answer has a 'suggested edit queue is full' flag]

Barca answered 2/11, 2020 at 20:27 Comment(0)
B
3

I had this same problem today when debugging a project and found that the project was referencing the .Net Framework version of RazorEngine instead of the .NetCore version.

After I removed that reference and used the correct packet, the application started working.

This would explain why your resolution of of creating a Web Api that targets 4.6.1 worked.

Brattice answered 20/11, 2019 at 21:20 Comment(2)
What do you mean by ..removed that reference and used the correct packet, the application started working ? There is no .NET core version of RazorEngine.. Can you provide a detailed answer here.Canopy
@Seany84: Same issue as Matt Small only I used Oracle.ManagedDataAccess (Old School .NET) instead of Oracle.ManagedDataAccess.Core so this isn't necessarily specific to the RazorEngine package. Also, I see both RazorEngine and RazorEngine.NetCore in NuGet. My project is targeting .NET Core 3.1Intramolecular

© 2022 - 2024 — McMap. All rights reserved.