Permalink
Browse files

Switched to a lock fix, rather than creating multiple providers

  • Loading branch information...
1 parent 17b3c0e commit b4da5207ffb5c392752816ee3d40c39656d6c6d5 @grumpydev grumpydev committed Oct 3, 2012
@@ -1,5 +1,6 @@
namespace Nancy.ViewEngines.Razor.CSharp
{
+ using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Web.Razor;
@@ -11,7 +12,7 @@
/// <summary>
/// Renderer for CSharp razor files.
/// </summary>
- public class CSharpRazorViewRenderer : IRazorViewRenderer
+ public class CSharpRazorViewRenderer : IRazorViewRenderer, IDisposable
{
/// <summary>
/// Gets the assemblies.
@@ -32,12 +33,9 @@ public string Extension
public RazorEngineHost Host { get; private set; }
/// <summary>
- /// Creates the provider that is used to generate code.
+ /// Gets the provider that is used to generate code.
/// </summary>
- public CodeDomProvider CreateProvider()
- {
- return new CSharpCodeProvider();
- }
+ public CodeDomProvider Provider { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="CSharpRazorViewRenderer"/> class.
@@ -49,9 +47,23 @@ public CSharpRazorViewRenderer()
typeof(Microsoft.CSharp.RuntimeBinder.Binder).GetAssemblyPath()
};
+ this.Provider = new CSharpCodeProvider();
+
this.Host = new NancyRazorEngineHost(new CSharpRazorCodeLanguage());
this.Host.NamespaceImports.Add("Microsoft.CSharp.RuntimeBinder");
}
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ /// <filterpriority>2</filterpriority>
+ public void Dispose()
+ {
+ if (this.Provider != null)
+ {
+ this.Provider.Dispose();
+ }
+ }
}
}
@@ -27,6 +27,6 @@ public interface IRazorViewRenderer
/// <summary>
/// Gets the provider that is used to generate code.
/// </summary>
- CodeDomProvider CreateProvider();
+ CodeDomProvider Provider { get; }
}
}
@@ -7,21 +7,20 @@
using System.IO;
using System.Linq;
using System.Reflection;
- using System.Text;
using System.Web.Razor;
using System.Web.Razor.Parser.SyntaxTree;
- using System.Web.Razor.Text;
using Nancy.Bootstrapper;
using Nancy.Responses;
/// <summary>
/// View engine for rendering razor views.
/// </summary>
- public class RazorViewEngine : IViewEngine
+ public class RazorViewEngine : IViewEngine, IDisposable
{
private readonly IRazorConfiguration razorConfiguration;
private readonly IEnumerable<IRazorViewRenderer> viewRenderers;
+ private readonly object compileLock = new object();
/// <summary>
/// Gets the extensions file extensions that are supported by the view engine.
@@ -134,7 +133,7 @@ private Func<NancyRazorViewBase> GetCompiledViewFactory(string extension, TextRe
var razorResult = engine.GenerateCode(reader, sourceFileName:"placeholder");
- var viewFactory = this.GenerateRazorViewFactory(renderer.CreateProvider(), razorResult, referencingAssembly, renderer.Assemblies, passedModelType, viewLocationResult);
+ var viewFactory = this.GenerateRazorViewFactory(renderer.Provider, razorResult, referencingAssembly, renderer.Assemblies, passedModelType, viewLocationResult);
return viewFactory;
}
@@ -178,8 +177,12 @@ private Func<NancyRazorViewBase> GenerateRazorViewFactory(CodeDomProvider codePr
var compilerParameters = new CompilerParameters(assemblies.ToArray(), outputAssemblyName);
- var results = codeProvider.CompileAssemblyFromDom(compilerParameters, razorResult.GeneratedCode);
-
+ CompilerResults results;
+ lock (this.compileLock)
+ {
+ results = codeProvider.CompileAssemblyFromDom(compilerParameters, razorResult.GeneratedCode);
+ }
+
if (results.Errors.HasErrors)
{
var fullTemplateName = viewLocationResult.Location + "/" + viewLocationResult.Name + "." + viewLocationResult.Extension;
@@ -355,5 +358,22 @@ private NancyRazorViewBase GetViewInstance(ViewLocationResult viewLocationResult
return view;
}
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ /// <filterpriority>2</filterpriority>
+ public void Dispose()
+ {
+ if (this.viewRenderers == null)
+ {
+ return;
+ }
+
+ foreach (var disposable in this.viewRenderers.OfType<IDisposable>())
+ {
+ disposable.Dispose();
+ }
+ }
}
}
@@ -1,5 +1,6 @@
namespace Nancy.ViewEngines.Razor.VisualBasic
{
+ using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Web.Razor;
@@ -9,7 +10,7 @@
/// <summary>
/// Renderer for Visual Basic razor files.
/// </summary>
- public class VisualBasicRazorViewRenderer : IRazorViewRenderer
+ public class VisualBasicRazorViewRenderer : IRazorViewRenderer, IDisposable
{
/// <summary>
/// Gets the assemblies.
@@ -32,10 +33,7 @@ public string Extension
/// <summary>
/// Gets the provider that is used to generate code.
/// </summary>
- public CodeDomProvider CreateProvider()
- {
- return new VBCodeProvider();
- }
+ public CodeDomProvider Provider { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="VisualBasicRazorViewRenderer"/> class.
@@ -44,8 +42,21 @@ public VisualBasicRazorViewRenderer()
{
this.Assemblies = new List<string>();
+ this.Provider = new VBCodeProvider();
+
this.Host = new NancyRazorEngineHost(new VBRazorCodeLanguage());
}
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ /// <filterpriority>2</filterpriority>
+ public void Dispose()
+ {
+ if (this.Provider != null)
+ {
+ this.Provider.Dispose();
+ }
+ }
}
}

0 comments on commit b4da520

Please sign in to comment.