Asp.net mvc 佩雷斯特城堡不认识

Asp.net mvc 佩雷斯特城堡不认识,asp.net-mvc,castle-windsor,perwebrequest,Asp.net Mvc,Castle Windsor,Perwebrequest,新来的城堡/温莎,请容忍我 我目前正在使用该框架,在其启动代码中,它注册了HttpContextBase,如下所示: container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current))); container.Register(Component.For<H

新来的城堡/温莎,请容忍我

我目前正在使用该框架,在其启动代码中,它注册了HttpContextBase,如下所示:

container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
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;
        }
    }
container.Register(Component.For().lifety.Transient.UsingFactoryMethod(()=>newhttpcontextwrapper(HttpContext.Current));
我想做的是改变httpContextBase的行为和生活方式,使之成为PerWebRequest

因此,我将代码更改为以下内容:

container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
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;
        }
    }
container.Register(Component.For();
但是,当我这样做时,我得到了以下错误:

 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.Configuration.ConfigurationErrorsException:看起来您忘记了
注册http模块Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule
将“”添加到web.config上的节
这是我在
下做的,但是,我仍然得到相同的错误。有什么提示吗

提前谢谢

更新

为每个请求添加代码块

在system.web.mvc.extensibility框架中,有一个名为extendedmvcapapplication的类,它继承自HttpApplication,在Application_start方法中,它调用BootStrapper.Execute()。此方法的实现如下所示:

container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
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;
        }
    }
public void Execute()
{
bool shouldSkip=false;
foreach(ServiceLocator.GetAllInstances().OrderBy中的IBMootTrapperTask任务(任务=>task.Order))
{
如果(应该跳过)
{
shouldSkip=false;
继续;
}
TaskContinuation=task.Execute(ServiceLocator);
if(continuation==TaskContinuation.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"));
    }
}
公共类registerOutes:registerOutesBase
{
私有HttpContextBase-contextBase;
受保护的重写TaskContinuation ExecuteCore(IServiceLocator服务定位器)
{
contextBase=serviceLocator.GetInstance();
返回base.ExecuteCore(serviceLocator);
}
受保护覆盖无效寄存器(路由收集路由)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.IgnoreRoute(“{*favicon}”,new{favicon=@”(.*/)?favicon.ico(/.*)”});
IgnoreRoute(“{*robotstxt}”,新的{robotstxt=@”(.*/)?robots.txt(/.*)”});
SetAppRoutes(routes,contextBase.Server.MapPath(“~/Configuration/routes.xml”);
}
}

您可以看到,我需要获取httpcontextbase对象的实例(解析),以便获取xml文件的服务器路径。

可以尝试将其添加到模块部分,如下所示:


在撰写本文时,PerWebRequest生活方式不支持在应用程序_Start()中解析

请参见问题描述和讨论:

此特定情况的解决方法:

  • 将Register
    registerOutes
    作为实例,显式地将当前上下文作为构造函数参数传递给它,例如:

    container.Register(Component.For<IBootstrapperTask>()
                                .Instance(new RegisterRoutes(Context)));
    

  • 然后将
    IServerMapPath
    注入到
    注册表项中

    何时解析该对象?在哪一点上?在应用程序_Start()上,这是否太早了?@Herman:现在不支持。您试图在应用程序_Start()中解决什么问题?@Mauricio:在system.web.mvc.extensibility框架中,它允许您提前完成一系列任务。我实现的任务之一是连接所有路由值,这取决于httpcontext来查找xml文件的服务器路径。我现在的解决方法是使用tranisent而不是perwebrequest,这对我的场景很有效。@Herman:好的,但是为什么要在应用程序_Start()中解决这个问题呢?你能发布一些代码吗?