城堡/温莎的新手,请多多包涵。
我目前正在使用框架System.Web.Mvc.Extensibility并且在其启动代码中,它注册了 HttpContextBase ,如下所示:
container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
我想做的是改变行为并将httpContextBase的生活方式改变为PerWebRequest。
所以我将代码更改为以下内容:
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
但是,当我这样做时,出现以下错误:
System.Configuration.ConfigurationErrorsException: Looks like you forgot to
register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule
Add '<add name="PerRequestLifestyle"
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel"
/>' to the <httpModules> section on your web.config
我在 下做过<system.web>
,<system.webServer>
但是,我仍然遇到同样的错误。有什么提示吗?
提前致谢。
更新
每个请求添加代码块
在system.web.mvc.extensibility框架中,有一个继承自HttpApplication的类extendedMvcApplication,在Application_start方法中调用了BootStrapper.Execute()。此方法的实现如下:
public void Execute()
{
bool shouldSkip = false;
foreach (IBootstrapperTask task in ServiceLocator.GetAllInstances<IBootstrapperTask>().OrderBy(task => task.Order))
{
if (shouldSkip)
{
shouldSkip = false;
continue;
}
TaskContinuation continuation = task.Execute(ServiceLocator);
if (continuation == TaskContinuation.Break)
{
break;
}
shouldSkip = continuation == TaskContinuation.Skip;
}
}
如您所见,它遍历 IBootStrapperTask 列表并尝试执行它们。碰巧我有一项任务在我的 mvc 应用程序中注册路由:
public class RegisterRoutes : RegisterRoutesBase
{
private HttpContextBase contextBase;
protected override TaskContinuation ExecuteCore(IServiceLocator serviceLocator)
{
contextBase = serviceLocator.GetInstance<HttpContextBase>();
return base.ExecuteCore(serviceLocator);
}
protected override void Register(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
routes.IgnoreRoute("{*robotstxt}", new { robotstxt = @"(.*/)?robots.txt(/.*)?" });
XmlRouting.SetAppRoutes(routes, contextBase.Server.MapPath("~/Configuration/Routes.xml"));
}
}
您可以看到我需要 getInstance(resolve) 一个 httpcontextbase 对象,以便我可以获取 xml 文件的服务器路径。