Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 971621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:59:34+00:00 2026-05-16T02:59:34+00:00

I have a situation where I user IoC (WindsorContainer) in a .Net web application,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:59:35+00:00Added an answer on May 16, 2026 at 2:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where in a web application a user may need a
I have a situation where i have to take input(form) from user. After continue
Using c#, .net, Visual Studio 2010 I have a situation when a user clicks
I have this situation in my SL4 application: We create some User Accounts in
I have a situation where information about a user is stored in the web
Something similar has been asked in maven user list but I have the situation,
I have a situation where a user is attaching files within an application, these
i have a situation,i am creating a folder everytime when new user register.the folder
In my project, i have a situation like when user runs the application. The
We have a situation that need to user web form just like an ExcelSheet,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.