4

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.

clpx
  • 103
  • 2
  • 7

2 Answers2

2

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.

clpx
  • 103
  • 2
  • 7
0

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.

Faster Solutions
  • 7,005
  • 3
  • 29
  • 45
  • The documentation link you pointed to for the older config schema, and is clearly marked as obsolete at the top of the page. You can specify hierarchical in the config, it's a default alias. See [the list of default aliases](http://msdn.microsoft.com/en-us/library/ff660933(v=pandp.20).aspx) for more details. – Chris Tavares Apr 05 '12 at 02:48
  • Well thanks for help, buy i can for sure use hierarchy lifetime manager. Finaly it was caused by my stupidity, because i am using more db context and they was badly configured. – clpx Apr 06 '12 at 12:28
  • what was the solution? You should post it as an answer to your own question – Faster Solutions Apr 09 '12 at 20:11