Skip to content

Commit 3a3445c

Browse files
BeniGemperleBeniGemperle
authored andcommitted
Updated to Version 1.41 with razorEngine 3.9.3 and Template-Caching
1 parent 82805bf commit 3a3445c

File tree

13 files changed

+59
-15224
lines changed

13 files changed

+59
-15224
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,6 @@ $RECYCLE.BIN/
154154

155155
# Mac desktop service store files
156156
.DS_Store
157+
158+
.vs
159+
packages/

Description.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Group: Misc
22
FriendlyName: Razor MessageService
33
SystemName: ToSIC.RazorMessageService
4-
Version: 1.40
4+
Version: 1.41
55
SupportedVersions: 3.80
66
Author: 2sic Internet Solutions
77
DisplayOrder: 1

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.4.0.0")]
36-
[assembly: AssemblyFileVersion("1.4.0.0")]
35+
[assembly: AssemblyVersion("1.4.1.0")]
36+
[assembly: AssemblyFileVersion("1.4.1.0")]

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ How to Install
106106
107107
Version
108108
----
109+
###1.41
110+
* Updated to RazorEngine 3.9.3
111+
* Improved Performance with TemplateCaching. First use of a Mail-Template might take up to 2 Seconds to compile. But afterward no more recompilation is needed. Updating the mail template will cause a re-compilation.
112+
* Works with nopCommerce 3.80
113+
109114
###1.40
110115
* Works with nopCommerce 3.80
111116

RazorWorkflowMessageService.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
using Nop.Services.Events;
1717
using Nop.Services.Localization;
1818
using Nop.Services.Stores;
19-
using Nop.Services.Messages; // customized
19+
using Nop.Services.Messages;
20+
// customized
21+
using RazorEngine;
22+
using RazorEngine.Templating;
23+
using System.Security.Cryptography;
24+
using System.Text;
2025

2126
namespace ToSic.Nop.Plugins.RazorMessageService
2227
{
@@ -89,27 +94,30 @@ protected virtual int SendNotification(MessageTemplate messageTemplate,
8994
var subject = messageTemplate.GetLocalized(mt => mt.Subject, languageId);
9095
var body = messageTemplate.GetLocalized(mt => mt.Body, languageId);
9196

92-
//Replace subject and body tokens
93-
var subjectReplaced = _tokenizer.Replace(subject, tokens, false);
94-
var bodyReplaced = _tokenizer.Replace(body, tokens, true);
95-
9697
#region Customized
98+
// Run Razor befor tokenizer to prevent unneded recompilation of the Razor-Template which would be very resource intensive
99+
97100
// Razor-Parse Subject
98101
bool subjectSuccess;
99-
var subjectParsed = RazorParseSafe(subjectReplaced, razorModel, out subjectSuccess);
102+
var subjectRazorParsed = RazorParseSafe(messageTemplate.Id, subject, razorModel, out subjectSuccess);
100103
if (subjectSuccess)
101-
subjectReplaced = subjectParsed;
104+
subject = subjectRazorParsed;
102105
else
103-
subjectReplaced += subjectParsed;
106+
subject += subjectRazorParsed; // in case of an error, append the error-text returned
107+
104108
// Razor-Parse Body
105109
bool bodySuccess;
106-
var bodyParsed = RazorParseSafe(bodyReplaced, razorModel, out bodySuccess);
110+
var bodyRazorParsed = RazorParseSafe(messageTemplate.Id, body, razorModel, out bodySuccess);
107111
if (bodySuccess)
108-
bodyReplaced = bodyParsed;
112+
body = bodyRazorParsed;
109113
else
110-
bodyReplaced += bodyParsed;
114+
body += bodyRazorParsed; // in case of an error, append the error-text returned
111115
#endregion
112116

117+
//Replace subject and body tokens
118+
var subjectReplaced = _tokenizer.Replace(subject, tokens, false);
119+
var bodyReplaced = _tokenizer.Replace(body, tokens, true);
120+
113121
var email = new QueuedEmail
114122
{
115123
Priority = QueuedEmailPriority.High,
@@ -137,15 +145,38 @@ protected virtual int SendNotification(MessageTemplate messageTemplate,
137145
}
138146

139147
#region Customized
148+
149+
/// <summary>
150+
/// work arounf MD5 has for razorengine caching.
151+
/// </summary>
152+
/// <param name="input"></param>
153+
/// <returns></returns>
154+
private static string GetMd5Hash(string input)
155+
{
156+
var md5 = MD5.Create();
157+
var inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
158+
var hash = md5.ComputeHash(inputBytes);
159+
var sb = new StringBuilder();
160+
foreach (byte t in hash)
161+
{
162+
sb.Append(t.ToString("X2"));
163+
}
164+
165+
return sb.ToString();
166+
}
167+
140168
/// <summary>
141169
/// Parse text with Razor and handle Template Exception
142170
/// </summary>
143-
private static string RazorParseSafe(string text, object model, out bool success)
171+
private static string RazorParseSafe(int templateId, string text, object model, out bool success)
144172
{
145173
string result;
146174
try
147175
{
148-
result = RazorEngine.Razor.Parse(text, model);
176+
var key = "MailTemplate" + templateId + GetMd5Hash(text);
177+
178+
result = Engine.Razor.RunCompile(text, key, model: model);
179+
149180
success = true;
150181
}
151182
catch (RazorEngine.Templating.TemplateCompilationException ex)
@@ -183,7 +214,7 @@ protected virtual EmailAccount GetEmailAccountOfMessageTemplate(MessageTemplate
183214
if (emailAccountId == 0)
184215
emailAccountId = messageTemplate.EmailAccountId;
185216

186-
var emailAccount = _emailAccountService.GetEmailAccountById(emailAccountId);
217+
var emailAccount = _emailAccountService.GetEmailAccountById(emailAccountId);
187218
if (emailAccount == null)
188219
emailAccount = _emailAccountService.GetEmailAccountById(_emailAccountSettings.DefaultEmailAccountId);
189220
if (emailAccount == null)

ToSic.Nop.Plugins.RazorMessageService.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
<HintPath>..\nopCommerce_3.80_NoSource\bin\Nop.Services.dll</HintPath>
5656
<Private>False</Private>
5757
</Reference>
58-
<Reference Include="RazorEngine">
59-
<HintPath>packages\RazorEngine.3.6.1\lib\net45\RazorEngine.dll</HintPath>
58+
<Reference Include="RazorEngine, Version=3.9.3.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
59+
<HintPath>packages\RazorEngine.3.9.3\lib\net45\RazorEngine.dll</HintPath>
6060
<Private>True</Private>
6161
</Reference>
6262
<Reference Include="System" />

packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net451" />
4-
<package id="RazorEngine" version="3.6.1" targetFramework="net451" />
4+
<package id="RazorEngine" version="3.9.3" targetFramework="net451" />
55
</packages>

packages/RazorEngine.3.6.1/LICENSE.md

Lines changed: 0 additions & 22 deletions
This file was deleted.
-281 KB
Binary file not shown.
-276 KB
Binary file not shown.

0 commit comments

Comments
 (0)