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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Ok, I believe this is the best way for accomplish that. Thank you!
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/

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.