【问题标题】:RazorEngine is slowRazorEngine 很慢
【发布时间】:2014-06-26 18:47:11
【问题描述】:

我有使用 RazorEngine 生成电子邮件的 MVC5 应用程序。每封电子邮件都在一个 cshtml 模板中。模板将数据传递给它们,因此即使我正在这样做,缓存它们也无济于事。我还能做些什么来减少渲染时间吗?在 Azure 服务器上渲染需要几秒钟。

@{
    ViewBag.Title = "_EmailResetPassword";
}

@{ var PasswordToken = Model.token; }
@{ var URLpath = Model.URLpath; }
@{ string link = string.Format("http://{0}/Account/ResetPassword/?tokenid={1}", URLpath, PasswordToken); }

<html>

<head>
<title></title>
<style type="text/css">
.auto-style1 {
    text-align: center;
}
.auto-style2 {
    background: white;
    text-align: left;
    font-size: 11.0pt;
    font-family: Helvetica, sans-serif;
    color: #4D4D4D;
}
</style>
</head>

    <body>
        <div class="auto-style1">

        <h3 class="auto-style2">Dear @Model.FirstName,</h3>

        <h3 class="auto-style2">We have reset your password at Eph Apparel for the username: @Model.UserName</h3>

        <h3 class="auto-style2">Please click on the following link to create a new password:</h3>

        <h3 style='text-align:center;background:white'><span lang=EN-CA
        style='font-size:11.0pt;font-family:"Helvetica","sans-serif"'>
        <a href="@link">Reset Password</a></span></h3>

        <h3 style='text-align:center;background:white'><span lang=EN-CA
        style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";color:#4D4D4D'>LIKE US ON </span>
        <span> <a href="https://www.facebook.com/ephapparel" target="_blank"><img alt="" width=25 height=22
        src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/fb_icon.png" ></a></span>
        &nbsp;<span lang=EN-CA style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";
        color:#4D4D4D'>&nbsp;FOLLOW US ON </span><span lang=EN-CA></span>
        <span><a href="https://www.twitter.com/ephApparel" target="_blank"><img alt="" width=25 height=23
        src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/twitter_icon.png"></a></span></h3>
        <br>
        <img alt="" src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/eph_logo.png">
        </div>

    </body>

</html>

后面的代码:

private string ResetPassword(Customer oCustomer, string oToken)
{
    string oURLpath = GetConfigSettingById(3);

    string template = System.IO.File.ReadAllText(HostingEnvironment.MapPath("/bin/EmailTemplates/_EmailResetPassword.cshtml"));
    string message = Razor.Parse(template, new { FirstName = oCustomer.FirstName, UserName = oCustomer.Email, token = oToken, URLpath = oURLpath }, "Reset");
    return message;
}

【问题讨论】:

  • RazorEngine 可能会很慢,这取决于您在做什么......我们可以看看一些代码来帮助评估吗?
  • 除了其他两个非常合适的问题外,您是否在 Visual Studio 中使用调试在本地运行此程序?如果您是,那么在本地开发过程中某事有多“慢”对任何事情都没有影响。性能测试的时间是在生产就绪的服务器上。
  • 这是在 Azure 上的生产服务器上运行的。渲染视图需要几秒钟。

标签: asp.net-mvc razorengine


【解决方案1】:
private string ResetPassword(Customer oCustomer, string oToken)
{
    string oURLpath = GetConfigSettingById(3);    
    string template = System.IO.File.ReadAllText(HostingEnvironment.MapPath("/bin/EmailTemplates/_EmailResetPassword.cshtml"));
    string message = Razor.Parse(template, new { FirstName = oCustomer.FirstName, UserName = oCustomer.Email, token = oToken, URLpath = oURLpath });
    return message;
}

我要冒昧地说这就是你的速度变慢的地方......这些类型的操作在网站上非常昂贵,特别是如果它在其他地方运行(即 Azure)。这个操作实际上是加载整个文件,然后在每次重置密码时填充它。

通常有 3 个选项可以处理电子邮件。

  1. 保持原样(它有效,但速度慢)
  2. 采用模板/缓存方法(我将在几秒钟内完成,这会导致启动时显着变慢,但在运行时会比这好得多)
  3. 尝试 3rd 方 api(我发现 Postal 在我上一个网站上做类似的事情时在速度方面非常好)

要对其进行模板化,请执行以下操作:

  1. 首先,将 Razor Elements 变成一个新类。即:

    public class ResetPassword
    {
        public string FirstName { get; set; }
        public string Email { get; set; }
        public string Token { get; set; }
        public string UrlPath { get; set; }
    }
    
  2. 然后更改您的 html 以使用新类作为其模型:

    @model Your.Namespace.ResetPassword
    @{
        ViewBag.Title = "_EmailResetPassword";
        string link = string.Format("http://{0}/Account/ResetPassword/?tokenid={1}", Model.URLpath, Model.token); 
    }
    <html>   
     <head>
          <title></title>
          <style type="text/css">
          .auto-style1 {
             text-align: center;
          }
          .auto-style2 {
             background: white;
             text-align: left;
             font-size: 11.0pt;
             font-family: Helvetica, sans-serif;
             color: #4D4D4D;
          }
          </style>
    </head>
    <body>
        <div class="auto-style1">
    
        <h3 class="auto-style2">Dear @Model.FirstName,</h3>
    
        <h3 class="auto-style2">We have reset your password at Eph Apparel for the username: @Model.UserName</h3>
    
        <h3 class="auto-style2">Please click on the following link to create a new password:</h3>
    
        <h3 style='text-align:center;background:white'><span lang=EN-CA
        style='font-size:11.0pt;font-family:"Helvetica","sans-serif"'>
        <a href="@link">Reset Password</a></span></h3>
    
        <h3 style='text-align:center;background:white'><span lang=EN-CA
        style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";color:#4D4D4D'>LIKE US ON </span>
        <span> <a href="https://www.facebook.com/ephapparel" target="_blank"><img alt="" width=25 height=22
        src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/fb_icon.png" ></a></span>
        &nbsp;<span lang=EN-CA style='font-size:11.0pt;font-family:"Segoe UI","sans-serif";
        color:#4D4D4D'>&nbsp;FOLLOW US ON </span><span lang=EN-CA></span>
        <span><a href="https://www.twitter.com/ephApparel" target="_blank"><img alt="" width=25 height=23
        src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/twitter_icon.png"></a></span></h3>
        <br>
        <img alt="" src="http://ephoms-prod.azurewebsites.net/images/eph_graphics/eph_logo.png">
        </div>
    
    </body>
    

  3. 最后,回到您的代码,尝试以下操作:

      private string ResetPassword(Customer oCustomer, string oToken)
      {    
          string template = HostingEnvironment.MapPath("/bin/EmailTemplates/_EmailResetPassword.cshtml");
          ResetPassword password = new ResetPassword
          {
              FirstName = oCustomer.FirstName, 
              UserName = oCustomer.Email, 
              token = oToken, 
              URLpath = GetConfigSettingById(3)
          };
          var templateService = new TemplateService(); 
          return templateService.Parse(File.ReadAllText(template), password, null, "Reset");
      }
    

试试这个或第 3 方 api,让我们知道结果! (如果您需要有关邮政方面的帮助,请提出一个新问题,我很乐意提供一些意见)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2011-03-08
    • 2013-02-23
    • 2015-12-24
    相关资源
    最近更新 更多