How to RunComplie on updated template using same key in RazorEngine?
Asked Answered
B

1

5
string template = "Hello @Model.Name, welcome to RazorEngine!";
var result = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" });

Now i update my existing template to as below. I get my template from database.

string template = "Hello @Model.Name, welcome to My World!";

Whenever i do that i get an error The same key was already used for another template.

What is the best way to fix this issue?

Ballad answered 8/2, 2017 at 17:31 Comment(3)
Every template should have it's own unique key,. Check if the template has already been used with Engine.Razor.IsTemplateCached and use Run instead of RunCompile if it's already there.Disannul
@Disannul It would be great if you can give me an example of this.Ballad
See this for context - https://mcmap.net/q/1931779/-how-to-runcomplie-on-updated-template-using-same-key-in-razorengineDemmer
D
11

The issue is that you are not using a template key that is unique to the template code you are passing in. RazorEngine caches the templates and compiles them so the next time round it's faster to run.

var helloTemplate = "Hello @Model.Name";

string result;
var model = new { Name = "World" };

//Has the template already been used? If so, Run without compilation is faster
if(Engine.Razor.IsTemplateCached("helloTemplate", null))
{
    result = Engine.Razor.Run("helloTemplate", null, model);
}
else
{
    result = Engine.Razor.RunCompile(helloTemplate, "helloTemplate", null, model);
}
Disannul answered 8/2, 2017 at 18:5 Comment(6)
even if i update the template. It's still use the cache template when i run it second time. I am using LINQPad 4 for testing this issue.Ballad
If you update the template, then you need a new key - that is a different template. There's no way to clear the cache in RazorEngine as they are compiled as assemblies and it's impossible to unload an assembly. you could get round this by using a new engine instance: var engine = RazorEngineService.Create(); engine.RunCompile(...); but that will not release the memory from the previous template.Disannul
That mean every time i update the template i need to create a new key and compile it again. There is no way i can delete the old template from cache.Ballad
That's what I said, yes. The only way to clear the cache is to restart the AppDomain.Disannul
I found this link github.com/Antaris/RazorEngine/blob/master/doc/… . I update the code to use MyTemplateManager() and it's working fine. I am not sure if it's creating new key every time or using the same key and update new template. var config = new TemplateServiceConfiguration(); config.Debug = true; config.TemplateManager = new MyTemplateManager(template); Engine.Razor = RazorEngineService.Create(config); Engine.Razor.RunCompile(template, null, viewModel).Dump(); I can update template using same key.Ballad
That's basically doing the same thing as I said above, the templates are still being put into a cache.Disannul

© 2022 - 2024 — McMap. All rights reserved.