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

  • SEARCH
  • Home
  • 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 6249813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:18:27+00:00 2026-05-24T13:18:27+00:00

I am using Unity to instantiate some objects and I’m finding that no matter

  • 0

I am using Unity to instantiate some objects and I’m finding that no matter what I try, Unity is creating singletons for my objects.

According to the documentation:
http://msdn.microsoft.com/en-us/library/dd203242.aspx#Y500

With the following code I should be getting a different instance every time the interface gets resolved.

IUnityContainer myContainer = new UnityContainer();  

// Register a default (un-named) type mapping with a transient lifetime  
myContainer.RegisterType<IMyObject, MyRealObject>();  
// Following code will return a new instance of MyRealObject  
myContainer.Resolve<IMyObject>();  

But instead I’m getting a singleton instance.

Below is my declaration. Global.asax

// This should get me a singleton  
container.RegisterType<IRetailerService, RetailerService>(new ContainerControlledLifetimeManager(), new InjectionConstructor());  
// This is the one giving me trouble.
container.RegisterType<IInStoreRetailersViewModelBuilder, InStoreRetailersViewModelBuilder>(new InjectionConstructor(container.Resolve<IRetailerService>()));  
container.RegisterType<CollectController>(new InjectionConstructor(container.Resolve<IInStoreRetailersViewModelBuilder>()));  

Controller

private readonly IInStoreRetailersViewModelBuilder _inStoreRetailersViewModelBuilder;  

public CollectController(IInStoreRetailersViewModelBuilder inStoreRetailersViewModelBuilder)  
{  
    this._inStoreRetailersViewModelBuilder = inStoreRetailersViewModelBuilder;  
}  

public ActionResult Index()  
{  
    InStoreViewModel viewModel = this._inStoreRetailersViewModelBuilder.WithRetailers().WithPostcode().Build();  
}  

If I open Chrome and run the Index action, and then I go and open internet explorer and call the Index action, on the second call, in the constructor the inStoreRetailersViewModelBuilder parameter that gets injected is the one generated on the first call (with Chrome).

I’ve tried using the PerResolveLifetimeManager() and even the PerHttpRequestLifetime() from this thread: MVC, EF – DataContext singleton instance Per-Web-Request in Unity

But nothing seems to give me a brand new instance. Anyone can shed some light on what I may be doing wrong here?

  • 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-24T13:18:28+00:00Added an answer on May 24, 2026 at 1:18 pm

    Try this and see if it helps.

    container.RegisterType<IInStoreRetailersViewModelBuilder, InStoreRetailersViewModelBuilder>(
    new InjectionConstructor(
        new ResolvedParameter<IRetailerService>()));  
    container.RegisterType<CollectController>(
    new InjectionConstructor(
        new ResolvedParameter<IInStoreRetailersViewModelBuilder>()));  
    

    Maybe by resolving the parameter to your constructor yourself you are essentially passing in a specific instance which results in it being a singleton.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Unity documentation states: if a class that developers instantiate using the Resolve method
I've been using Unity for some time in my project. I have a singleton
If using Unity with WCF using an implementation such as that shown in this
Creating the View and View Model Using Unity Using Unity as your dependency injection
Using Unity in an ASP.Net MVC 2 app I have various dependencies on Controllers
I am using Unity with MVC and NHibernate. Unfortunately, our UnitOfWork resides in a
I'm trying to implement this scenario using Unity and i can't figure out how
My team is in the process of developing a system where we're using Unity
I am thinking about using Microsoft Unity for my Dependency Injection tool in our
I am having a problem using the Unity Application Block, I have created a

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.