I\'m using the RazorEngine library (http://razorengine.codeplex.com/) in an MVC 3 web application to parse strings (that aren\'t views) using the Razor templating language.<
Update: According to a blog post from their team, the latest version 3.x (on Github) is now thread-safe. I have not vetted the veracity of its thread-safety, but assume it has been implemented properly. Please consider the rest of this answer useful only for historical purposes.
Judging from the code, this project doesn't look remotely thread-safe.
Razor.Parse:
public static string Parse<T>(string template, T model, string name = null)
{
return DefaultTemplateService.Parse<T>(template, model, name);
}
TemplateService.Parse:
public string Parse<T>(string template, T model, string name = null)
{
var instance = GetTemplate(template, typeof(T), name);
...
}
TemplateService.GetTemplate:
internal ITemplate GetTemplate(string template, Type modelType, string name)
{
if (!string.IsNullOrEmpty(name))
if (templateCache.ContainsKey(name))
return templateCache[name];
var instance = CreateTemplate(template, modelType);
if (!string.IsNullOrEmpty(name))
if (!templateCache.ContainsKey(name))
templateCache.Add(name, instance);
return instance;
}
So, Razor.Parse
is a static method. DefaultTemplateService
is a static property on Razor
, and Parse
and GetTemplate
are instance methods, but effectively invoked statically because of the static DefaultTemplateService
. This means all threads go through the same instance and go through GetTemplate
. You'll notice that GetTemplate
mutates state (templateCache
) without acquiring any locks. Therefore, this code is not threadsafe.