seesharper/LightInject

Externally provided singleton instances are disposed on container disposal

timbussmann opened this issue · 0 comments

Using the Microsoft.Extensions.DependencyInjection integration, when registering an existing object instance as a singleton, it's dispose method is also invoked when the IServiceProvider is disposed although the instance has been created by the user and not by the container itself. The Microsoft default implementation does not dispose instances that have been registered this way but it doesn't seem to be a part of the "specs".

repro:

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton(new MySingleton());
var serviceProvider = serviceCollection.CreateLightInjectServiceProvider(new ContainerOptions().WithMicrosoftSettings());

var singleton = serviceProvider .GetService<MySingleton>();
(sp as IDisposable).Dispose(); // Dispose of MySingleton is called

Are there any plans to align the disposal behavior with Microsoft's default container implementation?