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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:34:05+00:00 2026-06-10T13:34:05+00:00

Is this normal to write the following code: protected void Application_BeginRequest() { var container

  • 0

Is this normal to write the following code:

protected void Application_BeginRequest()
{
    var container = new UnityContainer()
        .RegisterType<IUnitOfWork, MyEntities>()

    HttpContext.Current.Items["container"] = container;

    ServiceLocator.SetLocatorProvider(
        () => new UnityServiceLocator(container));
}

protected void Application_EndRequest()
{
    var container = HttpContext.Current.Items["container"] 
        as UnityContainer;

    if (container != null)
    {
        container.Dispose();
    }
}

Or registering dependencies for every request is a bad practice?

Thanks.

  • 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-06-10T13:34:06+00:00Added an answer on June 10, 2026 at 1:34 pm

    Note: the following advice holds for all IoC frameworks and is not specific to Unity.

    TLDR: don’t do this. Register the container once in Application_Start and call ServiceLocator.SetLocatorProvider just once.

    It is fine to register dependencies to have a ‘per request’ or ‘per web request’ lifetime. Creating a new container instance per web request however, is a very bad practice.

    Containers are optimized for resolving instances (not for registering), which often means there is some dynamic code generation going on under the covers when a type is requested (from the container) for the first time. The registration and code generation and compilation process can be pretty time consuming, and creating a new instance of the container on each web request, will trigger the complete registration and code generation process all over again. This takes time, generates many temporary objects (more garbage to be collected), and generates new code that must be cached at least during the lifetime of that container instance.

    Further more, having multiple container instances, makes it hard to do other optimizations, such as registering instances with a bigger scope than per web request. In your case, registering a type as ‘singleton’ practically means ‘per web request’, since that type is singleton within the context of that container instance. Or even without performance as a consideration, for some types to work correctly, you’ll need a lifetime larger than ‘per web request’ (such as one instance per app domain), which is harder to configure.

    You probably can do this, even with a container per request, but your code will be much harder to maintain.

    The container itself is thread-safe, so there is in that sense no need to have container instance per request.

    And in your case it is even worse, since you have a race condition, a concurrency bug, in your code, since you change the ServiceLocator on each request. The ServiceLocator stores the registered delegate in a private static field, which means it is accessible from all threads. In other words, you are sharing container instances over web requests. You probably won’t notice during development, since you will be having one web request at the time, but when you have code calling ServiceLocator.Current, it will fail in production. Take for instance the following very likely scenario where request 1 starts and sets the service locator. After that, request 2 starts and overrides the service locator with it’s own container instance. After that, code in request 1 calls ServiceLocator.Current which now returns the container of the second request. Sometimes (if you’re lucky) you’ll get an ObjectDisposedException. This will happen if a thread disposes its container that another thread is still (incorrectly) using. Sometimes however, types are resolved that belong to a different web request (instances that are unsafe to be used over multiple threads), which is obviously bad and causes all kinds of strange and incorrect behavior. For instance when you return a LINQ to SQL DataContext or Entity Framework ObjectContext instance. Those classes should normally be configured per web request and can’t be shared by multiple threads.

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

Sidebar

Related Questions

Take the following code snippet: <script> var x = 25; document.write(x << 9); </script>
I have this following piece of code: var stats = { ...., checkExistance :
My code looks close to this: Lots of writes using normal C++ IO stream
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter =
I have following piece of code: public void ProcessRequest (HttpContext context) { context.Response.ContentType =
I want to write code that compiles conditionally and according to the following two
My problem is the following - I have this lines of code: // allocate
Please have a look at the following code package normal; import java.io.*; import java.util.*;
I'm following this example code: http://support.microsoft.com/kb/871044 , but doing it in c#. I don't
from this (normal dataGrid) (source: narod.ru ) into this (horisontal data grid) How to

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.