Class BootstrapperLifetime
- Namespace
- Codebelt.Bootstrapper
- Assembly
- Codebelt.Bootstrapper.dll
Listens for Ctrl+C or SIGTERM and initiates shutdown.
public class BootstrapperLifetime : Disposable, IDisposable, IHostLifetime, IHostLifetimeEvents
- Inheritance
-
BootstrapperLifetime
- Implements
- Inherited Members
Examples
The following example shows how to resolve BootstrapperLifetime from the host's service container, attach a callback to the application-stopped event, and confirm the lifetime is registered after the host starts. The BootstrapperLifetime replaces the default Microsoft.Extensions.Hosting.Internal.ConsoleLifetime so that consumers can subscribe to the same IHostLifetimeEvents surface area used by hosted services.
using System;
using Codebelt.Bootstrapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BootstrapperLifetimeDemo;
public static class Program
{
public static void Main()
{
var builder = Host.CreateApplicationBuilder();
builder.UseBootstrapperLifetime();
var host = builder.Build();
host.Start();
var lifetime = (BootstrapperLifetime)host.Services.GetRequiredService<IHostLifetime>();
lifetime.OnApplicationStoppedCallback = () => Console.WriteLine("Stopped.");
host.StopAsync().GetAwaiter().GetResult();
}
}
Constructors
BootstrapperLifetime(IOptions<ConsoleLifetimeOptions>, IHostEnvironment, IHostApplicationLifetime, IOptions<HostOptions>, ILoggerFactory)
Initializes a new instance of the BootstrapperLifetime class.
public BootstrapperLifetime(IOptions<ConsoleLifetimeOptions> options, IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, IOptions<HostOptions> hostOptions, ILoggerFactory loggerFactory)
Parameters
optionsIOptions<ConsoleLifetimeOptions>The dependency injected IOptions<TOptions>.
environmentIHostEnvironmentThe dependency injected IHostEnvironment.
applicationLifetimeIHostApplicationLifetimeThe dependency injected IHostApplicationLifetime.
hostOptionsIOptions<HostOptions>The dependency injected IOptions<TOptions>.
loggerFactoryILoggerFactoryThe dependency injected ILoggerFactory.
Properties
OnApplicationStartedCallback
Triggered when the application host has fully started.
public Action OnApplicationStartedCallback { get; set; }
Property Value
OnApplicationStoppedCallback
Triggered when the application host has completed a graceful shutdown.///
public Action OnApplicationStoppedCallback { get; set; }
Property Value
OnApplicationStoppingCallback
Triggered when the application host is starting a graceful shutdown.
public Action OnApplicationStoppingCallback { get; set; }
Property Value
Methods
OnDisposeManagedResources()
Called when this object is being disposed by either Dispose() or Dispose(bool) having disposing set to true and Disposed is false.
protected override void OnDisposeManagedResources()
StopAsync(CancellationToken)
Called from StopAsync(CancellationToken) to indicate that the host is stopping and it's time to shut down.
public Task StopAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenUsed to indicate when stop should no longer be graceful.
Returns
WaitForStartAsync(CancellationToken)
Called at the start of StartAsync(CancellationToken) which will wait until it's complete before continuing. This can be used to delay startup until signaled by an external event.
public Task WaitForStartAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenUsed to abort program start.