城堡perrequestlifestyle不认识
-
28-09-2019 - |
题
新的是城堡/温莎,请忍受我。
我目前正在使用框架 system.web.mvc.stensensible 在其启动代码中,它注册了httpcontextbase,如下所示:
container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
我想做的是改变行为,并将httpcontextbase的生活方式更改为终止。
因此,我将代码更改为以下:
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.stensensensible框架中,有一个称为ExtendedMvCapplication的类,该类从httpapplication继承,在application_start方法中,它调用boottrapper.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"));
}
}
您可以看到我需要获得httpcontextbase对象(解决),以便我可以获取XML文件的服务器路径。
解决方案
截至撰写本文时,Perwebrequest的生活方式不支持在application_start()中解决。
请参阅问题描述和讨论:
- http://support.castleproject.org/projects/ioc/issues/view/ioc-issue-166
- http://groups.google.com/group/castle-project-users/browse_thread/thread/thread/d44d96f4b548611e
解决此特定情况的解决方法:
登记
RegisterRoutes
作为一个例子,将当前上下文作为构造函数参数,例如:container.Register(Component.For<IBootstrapperTask>() .Instance(new RegisterRoutes(Context)));
采用
HostingEnvironment.MapPath
代替contextBase.Server.MapPath
. 。想让它嘲笑吗?通过简单的接口使用它,例如:interface IServerMapPath { string MapPath(string virtualPath); } class ServerMapPath: IServerMapPath { public string MapPath(string virtualPath) { return HostingEnvironment.MapPath(virtualPath); } } container.AddComponent<IServerMapPath, ServerMapPath>();
然后注入 IServerMapPath
进入你的 RegisterRoutes
.