Using Razor in Parallel

Nov 25, 2010 at 8:25 PM

I've got a situation where I'd like to parse Razor templates inside a parallel loop.  This causes a collision of course when using the static Razor.Parse methods.  I can create an instance of a TemplateService inside my loop and Parse things out that way and it works fine.  However, this is going to cause issues if I want to use the pre-compilation feature down the road.  I can see a case where I would want to compile a template once then run it multiple times inside a parallel loop.  Since the template cache is per instance of TemplateService this isn't going to take advantage of pre-compilation.

Just a couple thoughts.  Don't know if you have any suggestions as to implementations I can do now for this situation.

BTW, excellent project! Well organized, and I love looking at your source.  Some of the best I've seen.

Nov 26, 2010 at 7:44 AM
Edited Nov 26, 2010 at 7:50 AM


Thanks for the kind comments, I'm especially proud of what we have done so far!

In respect to your issue, would it be beneficial if we changed the caching mechanism to use a concurrent dictionary?  The pre-compilation methods Compile and CompileWithAnonymous have been refactored into the TemplateService so pre-compilation can be done per each instance of the TemplateService.  The reason I moved this logic into a service class was to give maximum flexibility when it comes to handling base templates; obviously forcing a single base template through the static class would mean that you'd be forced to use that as the base template for all parsing. With a concurrent dictionary as the caching container would allow for shared static use without the need for managing our own lock states?