I have implemented custom lifetime provider in order to have singleton DB context per job in hangfire. The problem in code below is that created instances are not being released.
My question is: Why ReleaseObject
method is not called?
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);
}
}
}