views:

124

answers:

1

I have a situation where I user IoC (WindsorContainer) in a .Net web application, and in the Global.asax I register my container, but straight after I register my WindsorContainer I also need to Instantiate another Class (Oauth) in the Global.asax.

However because I use the Sharp Architecture and Nhibernate on IIS7 it makes the situation very tricky because the webSessionStorage gets registered during the Init() method in the Global.asax file, and I have to Instantiate the OauthInit class after the NhibernateSession has been initialised, however by this time the WindsorContainer is already null, since the Init occurs after the Application_Start. Here is the code:

  public class Global : HttpApplication, IOAuthServices
    {
        static ITokenRepository<AccessToken> _accessTokenRepository;
        static ITokenRepository<RequestToken> _requestTokenRepository;
        private IWindsorContainer _container;
        private WebSessionStorage _webSessionStorage;
        private IOAuthProvider _provider;

        public ITokenRepository<AccessToken> AccessTokenRepository
        {
            get { return _accessTokenRepository; }
        }

        public ITokenRepository<RequestToken> RequestTokenRepository
        {
            get { return _requestTokenRepository; }
        }


        public IOAuthProvider Provider
        {
            get { return _provider; }
        }

        void Application_Start(object sender, EventArgs e)
        {
            _requestTokenRepository = new InMemoryTokenRepository<RequestToken>();
            _accessTokenRepository = new InMemoryTokenRepository<AccessToken>();

            CreateWindsorContainer();
        }


        public override void Init()
        {
            base.Init();

            // The WebSessionStorage must be created during the Init() to tie in HttpApplication events
            _webSessionStorage = new WebSessionStorage(this);

        }

        /// <summary>
        /// Due to issues on IIS7, the NHibernate initialization cannot reside in Init() but
        /// must only be called once.  Consequently, we invoke a thread-safe singleton class to 
        /// ensure it's only initialized once.
        /// </summary>
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            NHibernateInitializer.Instance().InitializeNHibernateOnce(InitializeNHibernateSession);
            _provider = _container.Resolve<IInitOAuthProvider>("initOauth").OAuthProvider); // <-- THIS IS THE ISSUE HERE
        }

        /// <summary>
        /// If you need to communicate to multiple databases, you'd add a line to this method to
        /// initialize the other database as well.
        /// </summary>
        private void InitializeNHibernateSession()
        {
            NHibernateSession.Init(
                _webSessionStorage,
                new[] { Server.MapPath("~/bin/MyAppSuite.Data.dll") },
                new AutoPersistenceModelGenerator().Generate(),
                Server.MapPath("~/NHibernate.config"));
        }

    private void CreateWindsorContainer()
    {
        _container = new WindsorContainer();

        ComponentRegistrar.AddComponentsTo(_container);

        ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(_container));
    }


}

So I am really at the moment in a catch 22. When I am creating my WindsorContainer during the Application_Start request, the NHibernateSession has not been initialised, so I cannot call _container.Resolve<IInitOAuthProvider>("initOauth").OAuthProvider and when the NHibernateSession has been initialised, the _container object is null.

Any help would be appreciated, thanks.

+3  A: 

I think the main problem (as I think Mauricio Scheffer's comment is alluding to) is that there can be multiple instances of the HttpApplication object, and because of this there can be multiple copies of the IWindsorContainer _container and WebSessionStorage _webSessionStorage created, when (if I understand the code correctly) you only want a single instance. Also bare in mind your HttpApplication's Init() method can also get called multiple times because of this.

This is basically a fundamental misunderstanding of the HttpApplication's lifecycle. When incoming HTTP requests come into the application, the ASP.NET worker process will fire up multiple HttpApplication instances to deal with those requests, and once done will put them into a pool, ready to be used again (it won't destroy them). This is similar to database connection pooling.

But fundamentally you have to be aware that the HttpApplication IS NOT a Singleton, there can be multiple instances. This is quite a common mistake and has tripped me up as well, which is why I wrote a blog post on it.

Try setting your IWindsorContainer and WebSessionStorage as static.


EDIT: OK, more detailed answer having studied the code more carefully.

See your Application_Start event, in it you're initialising an instance of

private IWindsorContainer _container;

...but because it's not a static variable, it will only get initialised for the very first HttpApplication object that is instantiated. As additional HttpApplication objects get instantiated, their copies of the _container variable will be NULL. Why? Because the _container variable is only initialsed in the Application_Start event, which has already been fired. So _container will be set to an object for the first HttpApplication instance, but will be NULL for each HttpApplication instance after that.

Hope this makes sense.

Sunday Ironfoot
Thank you, very useful and good answer, and yes that fixes it. Much appreciated.
Ryk