未调用ITinyIoCObjectLifetimeProvider的ReleaseObject

时间:2016-11-14 19:55:47

标签: c# ioc-container hangfire tinyioc

我已经实现了自定义生命周期提供程序,以便在hangfire中作业具有单例数据库上下文。下面的代码中的问题是没有释放创建的实例。

我的问题是:为什么不调用ReleaseObject方法?

public class JobContextLifetimeProvider : TinyIoCContainer.ITinyIoCObjectLifetimeProvider
    {
        private static readonly ConcurrentDictionary<string, object> Locator = new ConcurrentDictionary<string, object>();

        public object GetObject()
        {
            var key = JobContext.JobId;
            return Locator.ContainsKey(key) ? Locator[key] : null;
        }

        public void SetObject(object value)
        {
            var key = JobContext.JobId;
            Locator[key] = value;

            if (value is IDataContext)
            {
                Debug.WriteLine("Created data context {0}", value.GetHashCode());
            }
        }

        public void ReleaseObject()
        {
            var item = GetObject() as IDisposable;

            if (item != null)
            {
                item.Dispose();
                SetObject(null);
            }
        }
    }

0 个答案:

没有答案