我在使用RazorEngine 3.4缓存时遇到了问题。我有几个电子邮件模板,它们都有相同的
我的问题是:有人遇到同样的问题吗?在它被修复之前(如果真的会修复),有什么“好”的解决方法?
更新:最新版本(目前为v.3.10)两个测试都通过了。所以这个问题已经被解决了。
@Layout
,但每个模板的Model
不同。一切正常,直到我尝试使用缓存,我读到了不要使用缓存:"将导致可怕的性能和内存泄漏"
来自这里。
所以我打开了它。这很简单,但引发了一个问题:当我尝试使用不同的Model解析另一个模板时,_Layout.cshtml
也会与第一个Model类型一起缓存,这将导致异常抛出:"System.ArgumentException: Object of type '....model1...' cannot be converted to type '...model2...'."
我在"IsolatedTemplateServiceTestFixture.cs"
中编写了两个单元测试来展示这个问题。第一个测试通过了,但第二个测试失败了,因为TemplateService.SetModelExplicit()
函数想要为Layout
设置不同的Model
类型的属性。
private Mock<ITemplateResolver> _templateResolver;
[Test]
public void IsolatedTemplateService_CanParseTemplateWithLayout_WithOneSerializableModels_UseCache()
{
_templateResolver = new Mock<ITemplateResolver>();
var config = new TemplateServiceConfiguration()
{
Resolver = _templateResolver.Object
};
using (var service = new TemplateService(config))
{
_templateResolver.Setup(i => i.Resolve("test")).Returns("<html>@RenderBody()</html>");
const string template = @"@{Layout=""test"";}<h1>Hello @Model.Item1</h1>";
const string expected = "<html><h1>Hello World</h1></html>";
var model = new Tuple<string>("World");
string result = service.Parse(template, model, null, "C1");
string result2 = service.Parse(template, model, null, "C1");
Assert.That(result == expected, "Result does not match expected: " + result);
Assert.That(result2 == expected, "Result does not match expected: " + result2);
}
}
[Test]
public void IsolatedTemplateService_CanParseTemplateWithLayout_WithDifferentSerializableModels_UseCache()
{
_templateResolver = new Mock<ITemplateResolver>();
var config = new TemplateServiceConfiguration()
{
Resolver = _templateResolver.Object
};
using (var service = new TemplateService(config))
{
_templateResolver.Setup(i => i.Resolve("test")).Returns("<html>@RenderBody()</html>");
const string template = @"@{Layout=""test"";}<h1>Hello @Model.Item1</h1>";
const string expected = "<html><h1>Hello World</h1></html>";
var model = new Tuple<string>("World");
string result = service.Parse(template, model, null, "C1");
string result2 = service.Parse(template, model, null, "C1");
const string template2 = @"@{Layout=""test"";}<h1>Hello2 @Model.Item1</h1>";
const string expected2 = "<html><h1>Hello2 123</h1></html>";
var model2 = new Tuple<int>(123);
string result3 = service.Parse(template2, model2, null, "C2");
Assert.That(result == expected, "Result does not match expected: " + result);
Assert.That(result2 == expected, "Result does not match expected: " + result2);
Assert.That(result3 == expected2, "Result does not match expected: " + result3);
}
}
我的问题是:有人遇到同样的问题吗?在它被修复之前(如果真的会修复),有什么“好”的解决方法?
更新:最新版本(目前为v.3.10)两个测试都通过了。所以这个问题已经被解决了。