4

Unity 出现配置错误。我正在尝试实现http://unitymvc3.codeplex.com/,但我现在被困住了,因为:

在我的统一配置中,我有以下设置:

<register type="IMainDbContext" mapTo="WorkflowContext">
    <lifetime type="hierarchical" />
    <constructor></constructor>
  </register>

但是在创建统一的时候,(我的简单代码在这里:)

UnityConfigurationSection section = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
        if (section != null)
        {
            section.Configure(container);
        }

        this.container = container;

一切都配置得很好,除了注册“IManDbContext”有LifetimeManagerType = {Name = "TransientLifetimeManager" FullName = "Microsoft.Practices.Unity.TransientLifetimeManager"},但它应该是hierarchical lifetime manager

你知道如何告诉统一(在配置中,而不是在代码中)我想要分层生命周期管理器吗?感谢您的任何提示。

4

2 回答 2

2

我的错误是由这个错误引起的:我有多个 DbContext,但它们配置错误:

<register type="IMainDbContext" mapTo="WorkflowContext">
    <lifetime type="hierarchical" />
    <constructor></constructor>
</register>

<register type="IReportDbContext" mapTo="SomeBadContext">
    <lifetime type="hierarchical" />
    <constructor></constructor>
</register>

当我使用这个配置时,这很糟糕,unity simple 不要配置任何生命周期管理器。在我正确设置这些上下文之后,unity 使用了我的生命周期管理器配置。

于 2012-04-10T07:19:59.380 回答
0

我不认为你可以。如果您要指定生命周期类型,则需要提供“单例”或“外部”(外部是自定义生命周期)。

链接到 Unity Schema 文档

公平地说,除非您使用多个 Unity 容器,否则我看不到拥有分层生命周期管理器的价值,因为这旨在确保您在主统一容器和任何子容器中仅实例化您的类型的一个实例你从中生成。

因此,除非您计划生成子容器并想要一个单独的 IMainDbContext 实例 inplmenting 对象,否则您不妨只使用“单例”生命周期管理器。

于 2012-04-03T09:08:36.500 回答