1

问题:

当我加载我的应用程序时,我试图多次初始化主控制器,我想知道为什么......这让我抓狂检查,我会同意的!

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;
    } 
4

2 回答 2

2

您的控制器将在涉及它的每个请求上进行初始化。

这是正常的,并且 IIS 是如何工作的。

于 2012-11-14T16:08:31.963 回答
0

也很高兴知道每个Unity Resolve默认情况下都会创建一个新的instance. 如果你不希望这样,你应该提供一个LifeTimeManager

阅读 Microsoft 关于了解 Lifetime ManagersUsing 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>();
于 2012-11-15T10:19:58.320 回答