0

I use Unity 2.0 with asp.net mvc3 in my project. An exception is thrown when I try to configure TestEntities : ObjectContext in my Repository class.

public class UserRepository:IUser
{
    //TestEntities ctx = new TestEntities();
    [Dependency]
    public TestEntities ctx { get; set; }
    //...
}

This is the exception message:

The type TestEntities has multiple constructors of length 1. Unable to disambiguate.

XML configuration:

<?xml version="1.0" encoding="utf-8" ?>
<unity xmlns="schemas.microsoft.com/practices/2010/unity">
    <container>
        <register type="DomainModel.Entity.TestEntities, DomainModel"
                  mapTo="DomainModel.Entity.TestEntities, DomainModel">
            <lifetime type="perthread"/>
        </register>
        <register type="DomainModel.Abstract.IUser, DomainModel"
                  mapTo="DomainModel.Concrete.UserRepository, DomainModel">
            <lifetime type="perthread"/>
        </register>
    </container>
</unity>
Mark Seemann
  • 209,566
  • 41
  • 390
  • 671

1 Answers1

3

That is completely incorrect configuration. First of all you are using per-thread lifetime. Per-thread lifetime is for scenarios where you control threading by yourselves but in ASP.NET MVC you don't have such control. ASP.NET MVC uses thread pool internally so threads are reused for subsequent requests = your context will be reused among request and it will cause you a lot of problems. Another problem is that per-thread lifetime will not handle disposal of the context so unless you handle it yourselves (that is pretty hard if you don't have thread lifetime under your control) your application will suffice big memory leaks.

You must use different lifetime management:

  • Per-resolve: That will create new instance each time you call Resolve on the container but in case of dependency hierarchy the same instance will be used for all injections. You must handle context disposal yourselves.
  • Transient: That will create new instance each time you call Resolve. If instance is needed more than once during dependency hierarchy it will create a new instance for each injection. You must handle context disposal yourselves.
  • Hierarchical: You must create new subcontainer for each request processing and resolve instance on this subcontainer. The same instance will be used each time you call Resolve on that subcontainer instance. You must dispose subcontainer once you are finished with the request and all instances with hierarchical lifetime will be disposed as well.
  • Custom lifetime manager like this per-request example but with correct dependency injection configuration this should not be needed and per-resolve or hierarchical manager should solve all your requirements.

More about different lifetime managers is in my article.

To your problem with TestEntities class. Unity will by default try to use constructor with largest number of parameters and resolve those parameters with dependency injection. If it will find more than one such constructor it will throw this error because it doesn't know which one to choose. Even if there will be only one you will get error because dependency for such constructor will not be resolved. You must explicitly tell Unity which constructor you want to call. This one will force Unity to use default constructor instead:

<register type="DomainModel.Entity.TestEntities, DomainModel"
          mapTo="DomainModel.Entity.TestEntities, DomainModel">
    <lifetime type="perresolve"/>
    <constructor />
</register>
Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • Thank you so much. Could you please give me more information about " handle context disposal" . – user1019359 Oct 31 '11 at 12:13
  • You simply have to dispose context once you don't need it any more and because the context should be used only for single logical operation (unit of work) it can be quite often. For example in web application or web service you usually have single context per request. In forms application you can have one context per form or presenter. – Ladislav Mrnka Oct 31 '11 at 12:18
  • Thank you. I really appreciate that if u have time to check another question in : [link](http://stackoverflow.com/questions/7953635/unity-2-0-ioc-configuration-about-generic-class) – user1019359 Oct 31 '11 at 12:38