castle PerRequestLifestyle not recognize

前端 未结 2 1638
轻奢々
轻奢々 2021-01-13 08:04

New to Castle/Windsor, please bear with me.

I am currently using the framework System.Web.Mvc.Extensibility and in its start up code, it registered HttpContextBase l

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 08:18

    As of this writing, PerWebRequest lifestyle does not support resolving in Application_Start().

    See issue description and discussion:

    • http://support.castleproject.org/projects/IOC/issues/view/IOC-ISSUE-166
    • http://groups.google.com/group/castle-project-users/browse_thread/thread/d44d96f4b548611e

    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()
                                  .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();
      

    Then inject IServerMapPath into your RegisterRoutes.

提交回复
热议问题