Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 478
Kam Avatar asked Nov 19 '18 15:11

Kam


2 Answers

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]

like image 65
Mark Avatar answered Sep 22 '22 00:09

Mark


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.

like image 29
Matt Small Avatar answered Sep 18 '22 00:09

Matt Small