Per Web Request support

Feb 21, 2011 at 11:59 PM
Edited Feb 22, 2011 at 12:02 AM

How are people currently using Unity on a per web request basis? Like Windor's PerWebRequest? In fact, Ninject seems to support it via the Request scope.

I want to be able to use UnitOfWork on a request by request basis.

Anyone? Ideas? Seems such a basic requirement that i must be missing something.

Thanks,
/steven

Feb 22, 2011 at 12:36 PM

There's no built-in lifetime manager for this but it can be accomplished using a custom lifetime manager.  Chris Tavares wrote a blog on how to create custom lifetime managers.

I haven't tried creating one but I believe you could store the items in the HttpContext.Current.Items object.  Several threads might give you more idea.

http://unity.codeplex.com/discussions/38588 

http://unity.codeplex.com/discussions/70993

http://unity.codeplex.com/discussions/28278

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 24, 2011 at 11:36 AM

weblivz,

IMO, the problem is that, if you register a IDisposable object, you will have to dispose of it at the end of the request, which is something I believe Unity cannot do on its own, so you will have to write an IHttpModule (any other solutions?).

RP

Feb 25, 2011 at 12:04 AM

You can iterate through the HttpContext.Current.Items and call dispose on those in the EndRequest handler.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 20, 2013 at 4:55 AM
Edited Feb 20, 2013 at 5:09 AM
I try to implement Per HttpRequest LifeTimeManager.

Here is the code :
        public sealed class PerHttpRequestLifetimeManager : LifetimeManager, IDisposable
        {
            private object _value;
            private Guid _perRequestGuid;

            public override object GetValue()
            {
                if (_perRequestGuid.Equals((Guid)HttpContext.Current.Items["guid"]))
                    return _value;

                _perRequestGuid = ((Guid)HttpContext.Current.Items["guid"]);
                return null;
            }

            public override void SetValue(object newValue)
            {
                RemoveValue();
                _value = newValue;
            }

            public override void RemoveValue()
            {
                if (_value != null)
                    Dispose();
            }

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

            private void Dispose(bool disposing)
            {
                if (disposing)
                {
                    IDisposable d = _value as IDisposable;
                    if (d != null)
                    {
                        d.Dispose();
                    }
                    _value = null;
                }
            }
        }
and in MvcApplication
    public class MvcApplication : System.Web.HttpApplication
    {
        public MvcApplication()
        {
            BeginRequest +=
                (a, b) =>
                    {
                        HttpContext.Current.Items["guid"] = Guid.NewGuid();
                    };           
        }
    }