1

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?

ab_732
  • 3,639
  • 6
  • 45
  • 61

2 Answers2

2

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.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • I agree. Certainly *not* session start, because that would either mean you reconfigure the whole system every time a new session is started, or would result in a container-per-session, which would lead to all sorts of hard to track problems. – Steven May 29 '11 at 01:38
  • Ok, I believe this is the best way for accomplish that. Thank you! – ab_732 May 31 '11 at 14:23
2

I think that PreApplicationStartMethod is a better place.

Check out these articles:

http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx

http://ilearnable.net/2010/11/22/webactivator-preapplicationstartmethod/

  • Interesting. I wonder what happens when a pre application start method fails. The problem with a failing Application_Start method is that the app domain stays alive, but leaves the system in an invalid (probably unconfigured) state. – Steven May 29 '11 at 01:44