diff --git a/Implab/ServiceLocator.cs b/Implab/ServiceLocator.cs --- a/Implab/ServiceLocator.cs +++ b/Implab/ServiceLocator.cs @@ -20,13 +20,11 @@ namespace Implab { public void Dispose() { if (shared) return; - if (cleanup != null) - cleanup(service); - else { - var d = service as IDisposable; - if (d != null) - d.Dispose(); - } + if (cleanup != null) { + if (service != null) + cleanup(service); + } else + Safe.Dispose(service); } #endregion @@ -173,10 +171,11 @@ namespace Implab { Unregister(typeof(T)); - m_services[typeof(T)] = new ServiceEntry { - activator = () => activator(), - cleanup = instance => cleanup((T)instance) - }; + var serviceEntry = new ServiceEntry(); + serviceEntry.activator = () => activator(); + if (cleanup != null) + serviceEntry.cleanup = instance => cleanup((T)instance); + m_services[typeof(T)] = serviceEntry; } public void Register(Func activator) {