gpt4 book ai didi

asp.net-mvc - 城堡 PerRequestLifestyle 无法识别

转载 作者:行者123 更新时间:2023-12-02 05:50:29 26 4
gpt4 key购买 nike

刚接触城堡/温莎,请耐心等待。

我目前正在使用框架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的类extendMvcApplication,在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 文件的服务器路径。

最佳答案

截至撰写本文时,PerWebRequest 生活方式不支持在 Application_Start() 中解析。

查看问题描述和讨论:

此特殊情况的解决方法:

  1. RegisterRoutes 注册为实例,显式地将当前上下文作为构造函数参数传递给它,例如:

    container.Register(Component.For<IBootstrapperTask>()
    .Instance(new RegisterRoutes(Context)));
  2. 使用HostingEnvironment.MapPath而不是contextBase.Server.MapPath。想让它变得可 mock 吗?通过简单的界面使用它,例如:

    interface IServerMapPath {
    string MapPath(string virtualPath);
    }

    class ServerMapPath: IServerMapPath {
    public string MapPath(string virtualPath) {
    return HostingEnvironment.MapPath(virtualPath);
    }
    }

    container.AddComponent<IServerMapPath, ServerMapPath>();

然后将 IServerMapPath 注入(inject)到您的 RegisterRoutes 中。

关于asp.net-mvc - 城堡 PerRequestLifestyle 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2670717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com