开发者

ASP.NET MVC 3 + Unity injection of the Controller

开发者 https://www.devze.com 2023-03-08 07:43 出处:网络
Which event is the best for registering the types with Unity? I wish to do this iocContainer.RegisterType<ControllerA>();

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 cau开发者_如何学运维sed 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?


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号