[英]MVC Controller initializer is being called many times
問題:
當我加載我的應用程序時試圖多次初始化主控制器,我想知道為什么......這讓我發瘋了,如果我們中的一個人有類似的錯誤並且想給我關於我必須做的事情的旅行檢查,我會同意的!!
MVC3 C# 使用 Unity 作為 IoC
控制器:
public ValorationController(IServiceProxy serviceProxy,
IHvmService hvmService,
IFamilyGroupService familyGroupService,
IClientService clientService,
IUserService userService,
IOfficeService delegationService,
ISocietyService societyService,
IFamilyService familyService,
IArticleService articleService,
IArticleFinishedService articleFinishedService,
IOrderService orderService)
: base(serviceProxy)
{
FamilyService = familyService;
ArticleService = articleService;
HvmService = hvmService;
FamilyGroupService = familyGroupService;
ClientService = clientService;
UserService = userService;
DelegationService = delegationService;
SocietyService = societyService;
ArticleFinishedService = articleFinishedService;
OrderService = orderService;
}
您的控制器將在涉及它的每個請求上初始化。
這是正常現象,也是 IIS 的工作原理。
也很高興知道每個Unity
Resolve
默認都會創建一個新instance
。 如果你不想那樣,你應該提供一個LifeTimeManager
閱讀 Microsoft 的關於“ 了解生命周期管理器”和“使用生命周期管理器”的文章。
也許你想使用這樣的東西:
// Register a default (un-named) type mapping with a singleton lifetime
myContainer.RegisterType<IMyObject, MySingletonObject>(new ContainerControlledLifetimeManager());
// Following code will return a singleton instance of MySingletonObject// Container will take over lifetime management of the object
myContainer.Resolve<IMyObject>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.