Search code examples
asp.net-mvc-3dependency-injectionunity-containerapplication-startcontroller-factory

ASP.NET MVC 3 + Unity injection of the Controller


Which event is the best for registering the types with Unity?

I wish to do this

        iocContainer.RegisterType<ControllerA>();
        iocContainer.RegisterType<ControllerB>();

so they could be retrieved by the ControllerFactory from the Unity Container.

My opinion was to do that in the Application_Start event, but I've been warned that I could face many problems caused by the App pool recycling (not firing the Application_start). So the alternative would be the Session_start.

Any advice?

[UPDATE]

But if I use

iocContainer.RegisterInstance<IService>(service)

what happens if the app pool recycle or IIS is resetted? Is the instance of service been recreated?


Solution

  • No, Application_start is the correct place to do it.


    Nothing's going to help if the app pool or IIS (or the server is recycled). Then the container will be recycled itself, but when the app pool is restarted, the container will be configured anew.