【问题标题】:'System.Security.Principal.WindowsImpersonationContext' from assembly 'mscorlib' error when trying to parse template with Razor Engine尝试使用 Razor 引擎解析模板时,来自程序集“mscorlib”的“System.Security.Principal.WindowsImpersonationContext”错误
【发布时间】:2019-04-21 23:28:46
【问题描述】:

我必须创建一个能够发送电子邮件的 Asp.Net Web Api。 我设法发送了电子邮件,但仅使用本地存储在变量中的简单模板。 下一步是从外部文件渲染模板,如下所示:

            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;
            }

问题是,当剃刀引擎尝试解析模板时,出现如下错误:

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

我试图从字符串中解析模板,我也被同样的错误阻止了。

【问题讨论】:

  • 您是否尝试过在视图顶部添加 using 语句? using System.Security 或 using System.Security.AccountManagement
  • 我添加了你的建议,没有任何反应。出现同样的错误。
  • 我通过创建一个针对 .Net Framework 4.6.1 的 Asp.Net 核心 Web Api 解决了这个问题。

标签: .net asp.net-web-api razorengine


【解决方案1】:

你可能会看到

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

或类似的 .csproj 文件,当你真正需要的是

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

所以,

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

(或与版本更新类似)看起来好像就是答案
[马特的回答有一个“建议的编辑队列已满”标志]

【讨论】:

    【解决方案2】:

    我今天在调试项目时遇到了同样的问题,发现该项目引用的是 RazorEngine 的 .Net Framework 版本,而不是 .NetCore 版本。

    在我删除该引用并使用正确的数据包后,应用程序开始工作。

    这可以解释为什么您创建针对 4.6.1 的 Web Api 的解决方案有效。

    【讨论】:

    • ..removed that reference and used the correct packet, the application started working 是什么意思? RazorEngine 没有 .NET 核心版本。你能在这里提供详细的答案吗?
    • @Seany84:与 Matt Small 相同的问题,只是我使用了 Oracle.ManagedDataAccess(Old School .NET)而不是 Oracle.ManagedDataAccess.Core,因此这不一定特定于 RazorEngine 包。此外,我在 NuGet 中同时看到了 RazorEngine 和 RazorEngine.NetCore。我的项目面向 .NET Core 3.1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多