Skip to content

Instantly share code, notes, and snippets.

@stevetalkscode
Last active Nov 14, 2020
Embed
What would you like to do?
Example of Disposing a Class Level object in StartUp using IHostApplicationLifetime
public class StartUp
{
private readonly SomeDisposable _someDisposable;
public StartUp()
{
_someDisposable = new SomeDisposable();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ISomeDisposable>(_someDisposable);
}
public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicationLifetime)
{
applicationLifetime.ApplicationStopping.Register(() => { _someDisposable.Dispose(); });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment