城堡PerRequestLifestyle无法识别

Her*_*man 5 asp.net-mvc castle-windsor perwebrequest

Castle/Windsor新手,请耐心等待.

我目前正在使用框架System.Web.Mvc.Extensibility,并在其启动代码中,它注册了HttpContextBase,如下所示:

container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Run Code Online (Sandbox Code Playgroud)

我想要做的是改变行为并将httpContextBase的生活方式改为PerWebRequest.

所以我将代码更改为以下内容:

container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,我收到以下错误:

 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
Run Code Online (Sandbox Code Playgroud)

这是我做下<system.web><system.webServer>,但是,我仍然得到同样的错误.任何提示?

提前致谢.

更新

每个请求添加了代码块

在system.web.mvc.extensibility框架中,有一个名为extendedMvc​​Application的类,它继承自HttpApplication,在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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

如您所见,它循环遍历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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以看到我需要getInstance(解析)一个httpcontextbase对象,这样我就可以得到一个xml文件的服务器路径.

Mau*_*fer 7

在撰写本文时,PerWebRequest生活方式不支持在Application_Start()中进行解析.

请参阅问题描述和讨论:

这种特殊情况的解决方法:

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

    container.Register(Component.For<IBootstrapperTask>()
                                .Instance(new RegisterRoutes(Context)));
    
    Run Code Online (Sandbox Code Playgroud)
  2. 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>();
    
    Run Code Online (Sandbox Code Playgroud)

然后注入IServerMapPath你的RegisterRoutes.