I have a problem with caching RazorEngine 3.4. I have several email templates with the same @Layoutbut different Modelsfor each template. It works fine until I try to use the cache that I read so as not to use the cache: "will result in both dreadful performances and memory leaks" from here .
So, I turned it on. It was simple, but led to a problem: it _Layout.cshtmlalso caches with the first type of model, when I try to parse another template with a different model, it throws an exception:"System.ArgumentException: Object of type '....model1...' cannot be converted to type '...model2...'."
I wrote 2 unit tests in "IsolatedTemplateServiceTestFixture.cs"to show the problem. The first one passes, but the second one fails because the function TemplateService.SetModelExplicit()wants to set the template.Model property with a different type Modelfor Layout.
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);
}
}
My question is: does anyone have the same problem? What would be a “good” way around it until it is fixed (if at all)?
Update:
With the latest version (currently v.3.10), both tests passed. Therefore, the problem has been fixed.