Skip to content

Instantly share code, notes, and snippets.

@stevetalkscode
Created Nov 14, 2020
Embed
What would you like to do?
Example of disposing an instance created inside the ConfigureServices method
public class StartUp
{
public StartUp()
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ISomeDisposable>(new SomeDisposable());
}
public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicationLifetime, IEnumerable<ISomeDisposable> someDisposables)
{
applicationLifetime.ApplicationStopping.Register(() =>
{
foreach (var disposable in someDisposables)
{
(disposable as IDisposable)?.Dispose();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment