45

A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate a unit of work per web request so that identity map is valid through out the request. This way I could use an IoC to inject my own IUnitOfWork to my repository classes transparently, and I could use the same instance to query and then update my entities.

Since I am using Unity, I mistakenly used PerThreadLifeTimeManager. I soon realised that ASP.Net threading model does not support what I want to achieve. Basically it uses a threadpool and recycles threads, and that means that I get one UnitOfWork per thread!! However, what I wanted was one unit of work per web request.

A bit of googling gave me this great post. That was exactly what I wanted; except for the unity part which was quite easy to achieve.

This is my implementation for PerCallContextLifeTimeManager for unity:

public class PerCallContextLifeTimeManager : LifetimeManager
{
    private const string Key = "SingletonPerCallContext";

    public override object GetValue()
    {
        return CallContext.GetData(Key);
    }

    public override void SetValue(object newValue)
    {
        CallContext.SetData(Key, newValue);
    }

    public override void RemoveValue()
    {
    }
}

And of course I use this to register my unit of work with a code similar to this:

unityContainer
            .RegisterType<IUnitOfWork, MyDataContext>(
            new PerCallContextLifeTimeManager(),
            new InjectionConstructor());

Hope it saves someone a bit of time.

CyclingFreak
  • 1,513
  • 2
  • 17
  • 38
Mehdi Khalili
  • 897
  • 1
  • 11
  • 18
  • Nice solution. If I may, I'd recommend renaming this to "CallContextLifetimeManager" since Web requests are probably only one of the potential applications. – Derek Greer Oct 20 '09 at 15:00
  • True, I updated the text and the code to reflect that. Thanks. – Mehdi Khalili Oct 21 '09 at 00:04
  • What's wrong with using PerResolveLifetimeManager? – Sleeper Smith Sep 13 '11 at 02:19
  • This is not a question!? – brumScouse Aug 21 '12 at 08:02
  • 3
    FYI, this is not the "correct" answer/solution to the problem. In ASP.NET, a single request can (and often will in heavy load) jump between threads. When it does, the CallContext is not carried over and only the HttpContext is migrated. If you want this to work in ASP.NET reliably (under load) you need to change CallContext to HttpContext.Current.Items instead. – Micah Zoltu Oct 31 '13 at 16:16

3 Answers3

25

Neat solution, but each instance of LifetimeManager should use a unique key rather than a constant:

private string _key = string.Format("PerCallContextLifeTimeManager_{0}", Guid.NewGuid());

Otherwise if you have more than one object registered with PerCallContextLifeTimeManager, they're sharing the same key to access CallContext, and you won't get your expected object back.

Also worth implementing RemoveValue to ensure objects are cleaned up:

public override void RemoveValue()
{
     CallContext.FreeNamedDataSlot(_key);
}
sixeyed
  • 282
  • 4
  • 2
  • -1 CallContext is recreated on each request. The key does not have to be unique between different instances of your per request singleton. – Igor Zevaka Oct 23 '10 at 01:51
  • 8
    +1 As a matter of fact, it has to be just like sixeyed said. If you don't assign unique keys then all the objects are registered under single key and things get messed up. – Élodie Petit Nov 13 '10 at 19:56
  • -1 See answer of Steven Robbins and comment of Micah Zoltu on question: under load `CallContext` is not preserved for a single request. – Frédéric Apr 03 '15 at 08:58
21

While this is fine calling this PerCallContextLifeTimeManager, I'm pretty sure this is not "safe" to be considered an ASP.Net Per-request LifeTimeManager.

If ASP.Net does its thread-swap then the only thing taken across to the new thread through CallContext is the current HttpContext - anything else you store in CallContext will be gone. This means under heavy load the code above could have unintended results - and I imagine it would be a real pain to track down why!

The only "safe" way to do this is with HttpContext.Current.Items, or doing something like:

public class PerCallContextOrRequestLifeTimeManager : LifetimeManager
{
    private string _key = string.Format("PerCallContextOrRequestLifeTimeManager_{0}", Guid.NewGuid());

    public override object GetValue()
    {
      if(HttpContext.Current != null)
        return GetFromHttpContext();
      else
        return GetFromCallContext();
    }

    public override void SetValue(object newValue)
    {
      if(HttpContext.Current != null)
        return SetInHttpContext();
      else
        return SetInCallContext();
    }

    public override void RemoveValue()
    {
    }
}

This obviously means taking dependencies on System.Web :-(

Much more information on this available at:

http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html

Steven Robbins
  • 25,817
  • 7
  • 71
  • 89
4

Thanks for your contribution,

But the question proposed implementation has two drawbacks, one of which is a serious bug as already stated by Steven Robbins in his answer and Micah Zoltu in a comment.

  1. Call context is not guaranteed to be preserved by Asp.Net for a single request. Under load, it can switch to another one, causing proposed implementation to break.
  2. It does not handle releasing of dependencies at request end.

Currently, Unity.Mvc Nuget package supplies a PerRequestLifetimeManager for doing the work. Do not forget to register its associated UnityPerRequestHttpModule in bootstrapping code otherwise dependencies releasing will not be handled either.

Using bootstrapping

DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

or in web.config system.webServer/modules

<add name="UnityPerRequestHttpModule" type="Microsoft.Practices.Unity.Mvc.UnityPerRequestHttpModule, Microsoft.Practices.Unity.Mvc" preCondition="managedHandler" />

It appears its current implementation is also suitable for web forms. And it does not even depend on MVC. Unfortunately, its assembly does, because of some other classes it contains.

Beware, in case you use some custom http module using your resolved dependencies, they may be already disposed in the module EndRequest. It depends on module execution order.

Frédéric
  • 8,372
  • 2
  • 51
  • 102