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 6624713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:41:13+00:00 2026-05-25T21:41:13+00:00

I have configured Unity in my ASP.NET application and the configuration is loaded when

  • 0

I have configured Unity in my ASP.NET application and the configuration is loaded when the first request is received in Application_BeginRequest. then the Unity container is stored in the Global.ascx as a property so that my other class can access it:

public static IUnityContainer ContainerHolder { get; set; }

IUnityContainer IContainerAccessor.Container
{
    get { return ContainerHolder; }
}

ContainerHolder, holds the container instance across application and Container property allows access to this property in each session.

Then I have a UnityLocator class which enables me access this property across the application:

public static class UnityLocator
    {        
        private static IUnityContainer Container
        {
            get
            {
                return ((IContainerAccessor)HttpContext.Current.ApplicationInstance).Container;
            }
        }
    }

Everything works fine!

I have also a method to access the instance from Unity:

UnityLocator.GetInstance<IThemeManager>();

    protected Repository(ICustomCacheManager customCacheManager)
    {
        this.Cache = customCacheManager;
    }

    protected Repository()
        : this(UnityLocator.GetInstance<ICustomCacheManager>())
    {

    }

this has been used in my app so that I can retrieve an existing instance from Unity so that I can inject it to other classes. For example my view (asp.net page) injects this to its Presenter class as a dependency.

Now, I’d like to configure my Unit tests to run.

How could I do that?! global.ascx doesn’t exist there obviously so I thought I should create a BaseTest class and let all my tests inherit it. then at the constructor of this BaseTest class, I build up my instances. Is it the right way to do it?

How to configure unit tests with Unity now?

Thanks

UPDATE:
UnityLocator.GetInstance added.

  • 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-25T21:41:13+00:00Added an answer on May 25, 2026 at 9:41 pm

    Probably using the global application class for storing the service locator was not a good idea. Why don’t you use the built-in ServiceLocator class? It is available from anywhere in the code and doesn’t depend on global application / HttpContext.

    Whether or not using the container in unit tests is another story. Personally I am not against it as long as you put stub implementations of your services into the container.

    Edit: the way to configure your container using ServiceLocator:

        private void ConfigureUnity()
        {
            UnityServiceLocator locator = new UnityServiceLocator( ConfigureUnityContainer() );
            ServiceLocator.SetLocatorProvider( () => locator );
        }
    
        private IUnityContainer ConfigureUnityContainer()
        {
            IUnityContainer container = new UnityContainer();
    
            // this loads container's configuration, comment or uncomment
            container.LoadConfiguration();
    
            return container;
        }
    

    You can then access the container from within the locator like:

    var container = ServiceLocator.Current.GetInstance<IUnityContainer>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have troubles consuming unity container in ASP.NET MVC3 application. I have several project
In my WCF web application I have configured the Unity container for Interception. Following
I have configured my Web.Config file as follow in a ASP.NET MVC 2 project:
I have an asp.net application and have used MVP pattern and created interfaces for
I have a unity container that's configured via a XML file. After it's configured
I have an ASP.NET MVC 3 application that uses the Ninject.MVC3 extension to setup
I have configured my application to use form based authentication and set up the
I've just started working on a brownfield ASP.NET 2.0 Web Application. As with a
I have installed Sonar and configured it to analyze our (.NET) projects (using Sonar-Runner).
I have been using the Windsor IoC Container for my web-based application, to resolve

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.