views:

104

answers:

1

I'm developing a web application using ASP.NET MVC and I'm using Windsor Castle as an IoC container (both WindsorControllerFactory and for my internal components). Currently I'm creating and storing the container into the Application (HttpApplicationState) like this:

    protected void Application_Start()
    {
        ...
        IWindsorContainer windsorContainer = new WindsorContainer();
        Application["WindsorContainer"] = windsorContainer;
        ...
    }

    protected void Application_End()
    {
        IWindsorContainer container = (IWindsorContainer)Application["WindsorContainer"];
        container.Dispose();

        Application["WindsorContainer"] = null;
    }

Is this a proper approach for applications which will run on Web farms? Are there any issues I need to be aware of? Is there a better place to put the container?

I know the Application state isn't shared across servers in a web farm, but I suppose this shouldn't be a problem as long as the data stored in the Application state isn't request-specific?

A: 

I prefer to store the container in a static gateway like Ayende's IoC from Rhino Commons

Mauricio Scheffer
Cross-cutting concerns can be resolved more elegantly than by coupling the code with static references, but anyway - this doesn't really have much to do with what I asked. Even if you do use a static gateway, it won't be preserved across requests.
Igor Brejc
This was an answer to your question "Is there a better place to put the container?". Anyway, the container is usually not exposed to the rest of the application, so it doesn't make much of a difference. And using a static IoC *does* preserve it across requests, since it's internally stored in a static variable...
Mauricio Scheffer