Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:55:00+00:00 2026-05-26T14:55:00+00:00

I use Unity 2.0 with asp.net mvc3 in my project. An exception is thrown

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:55:01+00:00Added an answer on May 26, 2026 at 2:55 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use templated helpers in an ASP.NET MVC 3 project. One display template had
We've been using Unity for our ASP.NET MVC3 applications. The configuration is done solely
I'd like to use Unity as an IoC container for an ASP.NET MVC 3
I have an ASP.Net MVC3 project that calls a WCF service to get data.
Context: Using Unity in (C#) ASP.NET MVC3 framework. Mark Seemann suggests In ASP.NET MVC
I need recommendations on a good Unit Testing book for use with ASP.NET MVC.
We are planning to use Unity in our project to dynamically load objects/types residing
I'm trying to get started with the repository pattern and ASP.NET MVC, and I
I'm working on an ASP.Net website along with a supporting Class Library for my
What are some good example (FOSS) ASp.NET MVC3 apps that come with unit tests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.