本文介绍了我应该UnityContainer.Teardown方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明确地放对象实例由Unity解决。我希望拆卸方法,应完全用于此,所以我想是这样的:

I would like to explicitly "release" object instance resolved by Unity. I hoped the Teardown method should be used exactly for this so I tried something like this:

container.RegisterType(typeof(IMyType), typeof(MyType), 
    new MyLifetimeManager());
var obj = container.Resolve<IMyType>();
...
container.Teardown(obj);

MyLifetimeManager 存储对象实例 HttpContext.Current.Items 。我预计,拆解方法调用 RemoveValue 终生经理和释放两个MyType的实例和寿命管理器实例。它不工作。首先 RemoveValue 不叫,如果我再打电话解析&LT; IMyType&GT; 我会得到previously解决实例。

MyLifetimeManager stores object instance in HttpContext.Current.Items. I expected that Teardown method will call RemoveValue on lifetime manager and release both MyType instance and lifetime manager instance. It doesn't work. First of all RemoveValue is not called and if I again call Resolve<IMyType> I will get previously resolved instance.

我应该拆解的方法吗?我怎样才能释放对象,尽管他生前的经理吗?

What should Teardown method do? How can I release object despite of his lifetime manager?

编辑:

如果拆解不释放的情况下,谁做?谁在叫 RemoveValue 终生经理吗?

If Teardown doesn't release the instance, who does? Who calls RemoveValue on lifetime manager?

推荐答案

统一TearDown中并没有做任何事情,开箱即用。你并不需要从HttpContext.Current.Items删除,因为它会自动在请求结束时清除。你可能想要做的就是调用Dispose存储有任何IDisposable的对象。你会在Global.asax中做到这一点从EndRequest:

Unity TearDown doesn't do anything out of the box. You do not need to remove from HttpContext.Current.Items as it will be cleared automatically at the end of the request. What you may want to do is call Dispose on any IDisposable object stored there. You would do this from EndRequest in Global.asax:

foreach (var item in HttpContext.Current.Items.Values)
            {
                var disposableItem = item as IDisposable;

                if (disposableItem != null)
                {
                    disposableItem.Dispose();
                }
            }

这篇关于我应该UnityContainer.Teardown方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 19:09