问题:
当我正在加载我的应用程序时,我试图多次初始化主控制器,我想知道为什么......这让我很疯狂,如果我们中的一个人有类似的错误并且想让我去旅行我必须检查,我会同意!!
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;
}
答案 0 :(得分:2)
您的控制器将在涉及它的每个请求上初始化。
这是正常的以及IIS如何工作。
答案 1 :(得分:0)
另外很高兴知道每个Unity
Resolve
默认情况下会创建一个新的instance
。如果您不想这样,则应提供LifeTimeManager
阅读Microsoft关于Understanding Lifetime Managers和Using Lifetime Managers的文章。
也许你想要使用这样的东西:
// 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>();