加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:08:37 所属栏目:asp.Net 来源:网络整理
导读:Castle / Windsor新手,请耐心等待. 我目前正在使用框架System.Web.Mvc.Extensibility并在其启动代码中,它注册了HttpContextBase,如下所示: container.Register(Component.ForHttpContextBase().LifeStyle.Transient.UsingFactoryMethod(() = new HttpContex
Castle / Windsor新手,请耐心等待.

我目前正在使用框架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框架中,有一个名为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;
        }
    }

如您所见,它循环遍历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(解析)一个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/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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读