0

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);
            }
        }
    }
Vasyl Senko
  • 1,779
  • 20
  • 33
  • Is `ReleaseObject` not being called? Or is the object returned by `GetObject()` not an `IDisposable`, and therefore, `SetObject(null)` never gets called when `ReleaseObject` is called? – wablab Nov 14 '16 at 20:02
  • ReleaseObject is not called, returned object is disposable (DbContext) – Vasyl Senko Nov 14 '16 at 20:10

0 Answers0