问题描述
|
哪个事件最适合在Unity中注册类型?
我希望这样做
iocContainer.RegisterType<ControllerA>();
iocContainer.RegisterType<ControllerB>();
因此,ControllerFactory可以从Unity容器中检索它们。
我的想法是在Application_Start事件中执行此操作,但是已警告我,我可能会遇到由应用程序池回收(不触发Application_start)引起的许多问题。因此,替代方法是Session_start。
有什么建议吗?
[更新]
但是如果我用
iocContainer.RegisterInstance<IService>(service)
如果应用程序池回收或IIS重置会发生什么?服务实例是否已重新创建?
解决方法
不,Application_start是正确的位置。
如果应用程序池或IIS(或服务器被回收),将无济于事。然后,容器本身将被回收,但是在重新启动应用程序池时,将重新配置容器。
,我认为PreApplicationStartMethod是一个更好的地方。
查看以下文章:
http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx
http://ilearnable.net/2010/11/22/webactivator-preapplicationstartmethod/