views:

1351

answers:

3

Hi,

A few days ago I had this 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 acheive. Basically it uses a theadpool 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 acheive.

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.

A: 

hey that's great. being lame, i solved this problem with a crappy dance. A service locator (simple wrapper around unity container) raised an event, asking for a place to store it's container. i registered this event in my global module, and fed it the ApplicationItems dictionary (speaking from memory here). it solved the problem, but yours is MUCH cleaner. thanks! i register types exclusively through config, and i'll assume you can set the lifetime manager in the config file. willl give it a try. many thanks for the post.

+5  A: 

Just wanted to flag this as answered! In fact, it was not a question; but I wanted this to be searchable for those who search StackOverFlow for technical questions!!

Mehdi Khalili
+2  A: 

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
-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