SPRNET-1443

-added ThreadSafeDependencyResolverRegistrar called from the HttpApplication.BeginRequest event to ensure that attempts to register the IApplicationContext initialization is properly delayed until the HttpContext.Request actually exists.  See http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx for details on this issue and the pattern implemented to address it.
This commit is contained in:
sbohlen
2011-06-02 02:36:28 +00:00
parent f71ab26b98
commit c732da2329
4 changed files with 64 additions and 33 deletions

View File

@@ -25,7 +25,7 @@
<spring>
<context>
<resource uri="~/Config/controllers.xml"/>
<resource uri="file://~/Config/controllers.xml"/>
</context>
</spring>

View File

@@ -29,35 +29,8 @@ namespace Spring.Context.Support
/// <summary>
/// Context Handler for ASP.NET MVC Applications
/// </summary>
public class MvcContextHandler : ContextHandler
public class MvcContextHandler : WebContextHandler
{
/// <summary>
/// The <see cref="System.Type"/> of <see cref="Spring.Context.IApplicationContext"/>
/// created if no <c>type</c> attribute is specified on a <c>context</c> element.
/// </summary>
/// <value></value>
protected override Type DefaultApplicationContextType
{
get { return typeof(MvcApplicationContext); }
}
/// <summary>
/// Get the context's case-sensitivity to use if none is specified
/// </summary>
/// <value></value>
/// <remarks>
/// <p>
/// Derived handlers may override this property to change their default case-sensitivity.
/// </p>
/// <p>
/// Defaults to 'true'.
/// </p>
/// </remarks>
protected override bool DefaultCaseSensitivity
{
get { return false; }
}
}
}

View File

@@ -65,6 +65,10 @@
<Name>Spring.Core.2010</Name>
<Private>True</Private>
</ProjectReference>
<ProjectReference Include="..\Spring.Web\Spring.Web.2010.csproj">
<Project>{BA4789EB-281A-48EA-8763-28B9F0596A18}</Project>
<Name>Spring.Web.2010</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />

View File

@@ -47,7 +47,29 @@ namespace Spring.Web.Mvc
//the Spring HTTP Module won't have built the context for us until now so we have to delay until the init
ConfigureApplicationContext();
RegisterResolver();
}
/// <summary>
/// Handles the BeginRequest event of the Application control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected virtual void Application_BeginRequest(object sender, EventArgs e)
{
var resolver = BuildDependencyResolver();
RegisterDependencyResolver(resolver);
}
/// <summary>
/// Builds the dependency resolver.
/// </summary>
/// <returns>The <see cref="IDependencyResolver"/> instance.</returns>
/// You must override this method in a derived class to control the manner in which the
/// <see cref="IDependencyResolver"/> is created.
protected virtual IDependencyResolver BuildDependencyResolver()
{
return new SpringMvcDependencyResolver(ContextRegistry.GetContext());
}
/// <summary>
@@ -62,7 +84,6 @@ namespace Spring.Web.Mvc
}
/// <summary>
/// Registers the DependencyResolver implementation with the MVC runtime.
/// <remarks>
@@ -70,9 +91,42 @@ namespace Spring.Web.Mvc
/// <see cref="SpringMvcDependencyResolver"/> is registered.
/// </remarks>
/// </summary>
public virtual void RegisterResolver()
public virtual void RegisterDependencyResolver(IDependencyResolver resolver)
{
DependencyResolver.SetResolver(new SpringMvcDependencyResolver(ContextRegistry.GetContext()));
ThreadSafeDependencyResolverRegistrar.Register(resolver);
}
/// <summary>
/// Thread-safe class that ensures that the <see cref="IDependencyResolver"/> is registered only once.
/// </summary>
protected static class ThreadSafeDependencyResolverRegistrar
{
private static bool _isInitialized = false;
private static readonly Object @lock = new Object();
/// <summary>
/// Registers the specified resolver.
/// </summary>
/// <param name="resolver">The resolver.</param>
public static void Register(IDependencyResolver resolver)
{
if (_isInitialized)
{
return;
}
lock (@lock)
{
if (_isInitialized)
{
return;
}
DependencyResolver.SetResolver(resolver);
_isInitialized = true;
}
}
}
}