문제

I have an configuration error with Unity. I am trying to implement http://unitymvc3.codeplex.com/, but i am stucked right now, because of this:

In my unity configuration I have this settings:

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

But at the time of creating unity, (my simple code is here:)

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

        this.container = container;

everything is configured great, except of registration "IManDbContext" which has LifetimeManagerType = {Name = "TransientLifetimeManager" FullName = "Microsoft.Practices.Unity.TransientLifetimeManager"}, but it should be hierarchical lifetime manager

Have you got any ideas how tell unity (in configuration, not in code) i want hierarchical lifetime manager? Thanks for any tips.

도움이 되었습니까?

해결책

My error was caused by this error: I have multiple DbContext, but they was badly configured:

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

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

When I was using this configuration, which was bad, unity simple don`t configure any lifetime manager. After I set these context right, unity used my lifetime manager configurations just right.

다른 팁

I don't think you can. If you're specifying the lifetime type you need to either supply "singleton" or "external" (external being a custom lifetime).

Link to Unity Schema Documentation

In fairness, unless you're using multiple Unity containers I don't see the value of having a hierarchical lifetime manager, as this is designed to ensure that you only have one instance of your type instantiated in the main unity container and any child containers you generate from it.

So, unless you're planning on generating child containers and want a separate instance of your IMainDbContext inplmenting object, you might as well just using "singleton" lifetime manager.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top