当webApi与HttpSelfHostServer一起运行时,有没有办法添加httpModule?

bra*_*ost 3 dependency-injection castle-windsor self-hosting asp.net-mvc-4 asp.net-web-api

我有一个MVC4 webapi控制器.它没有无参数构造函数.所以,我使用Windsor依赖解析器,在System.Web.Http.WebHost下工作时一切正常.但是,当我尝试使用HttpSelfHostServer时,我收到异常消息:

Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule To fix this add <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" /> to the <httpModules> section on your web.config. If you plan running on IIS in Integrated Pipeline mode, you also need to add the module to the <modules> section under <system.webServer>. Alternatively make sure you have Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 assembly in your GAC (it is installed by ASP.NET MVC3 or WebMatrix) and Windsor will be able to register the module automatically without having to add anything to the config file. 显然,windsor di无法注册他的HttpModule.我试过补充一下

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

在我的app.config中,没有运气.试过WebActivator使用PreApplicationStart方法也没有运气.有没有办法为自托管的webapi应用程序添加httpModule?

Dar*_*rov 7

自托管时无法使用HttpModules.它们是ASP.NET特有的功能.

  • @brainboost问题在于您的IoC容器.对于ASP.NET Web API,您不能依赖任何ASP.NET特定的东西,但您的IoC容器似乎正在做这件事.例如,Autofac有一个针对ASP.NET Web API的特定集成包,它们具有特定的"PerApiRequestLifetime"功能. (2认同)