本文主要是介绍IIS7集成模式初始化Spring.NET容器(Request is not available in this context exception in Application_Start问题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一般Spring容器是在执行第一个请求的时候触发的,但我碰到个需求,必须在应用程序启动的时候就要能够初始化Spring上下文。换句话说,我在Application_Start时就要能够执行
ContextRegistry.GetContext()
我使用了SignalR框架,为了和Spring一起协同工作,我需要使SignalR内置的IoC容器与Spring.NET容器协同工作(SignalR容器同时能够获取Spring容器中的对象,这不是本文重点)。
说一下本文出处(http://mvolo.com/iis7-integrated-mode-request-is-not-available-in-this-context-exception-in-applicationstart/)
解决的思路是将初始化工作延迟到第一次请求再执行(包括MVC注册路由等)
在global.asax中添加如下私有类
private class FirstRequestInitialization{private static bool s_InitializedAlready = false;private static Object s_lock = new Object();// Initialize only on the first requestpublic static void Initialize(HttpContext context){if (s_InitializedAlready){return;}lock (s_lock){if (s_InitializedAlready){return;}
//这里的内容就是原本要放在Application_Start中做的事情DependencyResolver.SetResolver(new SpringMvcDependencyResolver(ContextRegistry.GetContext()));AreaRegistration.RegisterAllAreas();RegisterGlobalFilters(GlobalFilters.Filters);RegisterRoutes(RouteTable.Routes);s_InitializedAlready = true;}}}
随后在覆盖BeginRequest方法
protected void Application_BeginRequest(object sender, EventArgs e)
{FirstRequestInitialization.Initialize(Context);
}
这篇关于IIS7集成模式初始化Spring.NET容器(Request is not available in this context exception in Application_Start问题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!