2013-12-19 82 views
3

我在我的MVC 3.0应用程序中使用castle windsor 3.1.0.0进行依赖注入。Castle Windsor 3.1 PerWebRequestLifestyleModule配置

我的容器是设置提供的控制器是这样的:

  container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestylePerWebRequest()); 

这似乎是工作,因为我看到每个请求创建一个新的控制器实例。然而根据documenation:http://docs.castleproject.org/Windsor.LifeStyles.ashx,我也必须把这个在我的web.config:

<httpModules> 
    <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor"/> 
</httpModules> 

,我没有。如果这个模块缺失,Castle Windsor的行为是什么? (文档中说,为了使每个Web请求正常运行,您必须在您的Web配置中具有此功能)。

回答

2

据我了解,在PerWebRequestLifestyle需要IHttpModule,以便它可以背驮式关闭init methodHttpApplication eventsBeginRequest

一切似乎工作的原因是因为该模块已被初始化,所以PerWebRequestLifestyle正常运行。

但是,如果您没有包含注册模块,为何会出现这种情况? 我怀疑这是一条传统指令,容器将尝试自行注册,但没有明确说明。

如果我们在CastleWindsor内窥视,我们会发现一种叫做Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModuleRegistration的东西。它有这种方法:

public static void Run() 
{ 
    Type type = Type.GetType("Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility, Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false); 
    if (type == null) 
    { 
     return; 
    } 
    MethodInfo method = type.GetMethod("RegisterModule", BindingFlags.Static | BindingFlags.Public); 
    if (method == null) 
    { 
     return; 
    } 
    object[] objArray = new object[] { typeof(PerWebRequestLifestyleModule) }; 
    method.Invoke(null, objArray); 
} 

什么是DynamicModuleUtility?快速搜索显示由K. Scott Allen所写的页面DynamicModuleUtility

DynamicModuleUtility将允许您在不对web.config文件进行任何更改的情况下将HTTP模块安装到ASP.NET管道中。

这只是我的猜测,究竟是怎么回事。您必须向Castle Windsor的创作者询问具体情况。

+0

这确实是这样的情况:http://stackoverflow.com/questions/18576350/perwebrequest-and-transient-lifestyles –