MVC Controller初始化程序被多次调用

时间:2012-11-14 16:07:02

标签: c# asp.net-mvc unity-container

问题:

当我正在加载我的应用程序时,我试图多次初始化主控制器,我想知道为什么......这让我很疯狂,如果我们中的一个人有类似的错误并且想让我去旅行我必须检查,我会同意!!

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

2 个答案:

答案 0 :(得分:2)

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

这是正常的以及IIS如何工作。

答案 1 :(得分:0)

另外很高兴知道每个Unity Resolve默认情况下会创建一个新的instance。如果您不想这样,则应提供LifeTimeManager

阅读Microsoft关于Understanding 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>();