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.
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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With