I define my DbConntextObj
_container.RegisterType<IDbConntextObj, DbConntextObj>(new HttpContextLifetimeManager<DbConntextObj>());
Unity does not call RemoveValue () for lifetimemanager
I have one dbcontext for multiple repositories.
My lifetimemanager is as follows:
public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable { private readonly string _itemName = typeof(T).AssemblyQualifiedName; public override object GetValue() { return HttpContext.Current.Items[_itemName]; } public override void RemoveValue() { var disposable = GetValue() as IDisposable; HttpContext.Current.Items.Remove(_itemName); if (disposable != null) disposable.Dispose(); } public override void SetValue(object newValue) { HttpContext.Current.Items[_itemName] = newValue; } public void Dispose() { RemoveValue(); } }
Is it bad that DbContext Dispose is not being called? Is there a workaround for Unity and MVC3?
source share