5

城堡/温莎的新手,请多多包涵。

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

4

2 回答 2

7

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

See issue description and discussion:

Workarounds for this particular case:

  1. Register RegisterRoutes as an instance, explicitly passing it the current context as constructor parameter, e.g.:

    container.Register(Component.For<IBootstrapperTask>()
                                .Instance(new RegisterRoutes(Context)));
    
  2. Use HostingEnvironment.MapPath instead of contextBase.Server.MapPath. Want to make it mockable? Use it through a simple interface, e.g.:

    interface IServerMapPath {
        string MapPath(string virtualPath);
    }
    
    class ServerMapPath: IServerMapPath {
        public string MapPath(string virtualPath) {
            return HostingEnvironment.MapPath(virtualPath);
        }
    }
    
    container.AddComponent<IServerMapPath, ServerMapPath>();
    

Then inject IServerMapPath into your RegisterRoutes.

于 2010-04-19T23:20:59.037 回答
0

Maby 尝试将其添加到 Modules 部分,如下所示: IIS7 & Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule 注册问题

http://www.jasonlinham.co.uk/2009/02/castle-and-iis7.html

于 2010-04-19T21:01:51.513 回答