我已经实现了自定义生命周期提供程序,以便在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);
}
}
}