How do I get the current Castle Windsor container?

后端 未结 2 797
孤独总比滥情好
孤独总比滥情好 2021-01-11 10:49

I am a Castle Winsor Noob. I have a WebForm project that is a hot mess. I am trying to resolve a dependency to test user registration. How do I get to the current Windsor

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-11 11:18

    There is interface in Windsor for this purpose. It is called IContainerAccessor. Best place to implement it is the Global.asax file:

    public class WebApplication : HttpApplication, IContainerAccessor {
      static IWindsorContainer container;
    
      public IWindsorContainer Container {
        get { return container; }
      }
    
      protected void Application_Start() {
        var bootstrapper = new WindsorConfigTask();
        bootstrapper.Execute();
        container = bootstrapper.Container; 
      }
    
      protected void Application_End() {
        container.Dispose();
      }
    }
    

    The usage in your web form is as following:

    var containerAccessor = Context.ApplicationInstance as IContainerAccessor;
    var container = containerAccessor.Container;
    

提交回复
热议问题