DbContext Unity不调用HttpContextLifetimeManager.RemoveValue()这是

本文关键字:这是 RemoveValue HttpContextLifetimeManager Unity 调用 DbContext | 更新日期: 2023-09-27 18:27:02

我正在定义我的DbConntextObj

_container.RegisterType<IDbConntextObj, DbConntextObj>(new HttpContextLifetimeManager<DbConntextObj>());

Unity没有在lifetimemanager 上调用RemoveValue()

我有一个用于多个存储库的Dbcontext。

我的终身经理人是这样的:

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();
        }
    }

没有调用DbContext Dispose是不是一件坏事?Unity和MVC3是否有变通方法?

DbContext Unity不调用HttpContextLifetimeManager.RemoveValue()这是

试试这个。

    public class MvcApplication : HttpApplication
{
    private IUnityContainer unityContainer;
    private HttpContextDisposableLifetimeManager ContextLifeTimeManager;
    /// <summary>
    /// The start method of the application.
    /// </summary>
    protected void Application_Start()
    {
        unityContainer = new UnityContainer();
        ContextLifeTimeManager = new HttpContextDisposableLifetimeManager();
        //for some reason this event handler registration doesn't work, meaning we have to add code to
        //Application_EndRequest as below...
        //this.EndRequest += new EventHandler(ContextLifeTimeManager.DisposingHandler);
        unityContainer.RegisterType<IUnitOfWork, EFUnitOfWork>(ContextLifeTimeManager);
        unityContainer.RegisterType<IRepository<ShoppingCart>, ShoppingCartRepository>(new ContainerControlledLifetimeManager());
    }
    //this seems hackish, but it works, so whatever...
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
        if (ContextLifeTimeManager != null)
        {
            ContextLifeTimeManager.RemoveValue();
        }
    }
}

然后在您的LifeTimeManager实现中。

public class HttpContextDisposableLifetimeManager : LifetimeManager, IDisposable
{        
    const string _itemName = typeof(T).AssemblyQualifiedName;
    public void DisposingHandler(object source, EventArgs e)
    {
        RemoveValue();
    }
    public override object GetValue()
    {
        return HttpContext.Current.Items[_itemName];
    }
    public override void RemoveValue()
    {
        Dispose();
        HttpContext.Current.Items.Remove(_itemName);
    }
    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[_itemName] = newValue;
    }
    public void Dispose()
    {
        var obj = (IDisposable)GetValue();
        obj.Dispose();
    }
}