Table of Contents

BootstrapperLifetime Class

Definition

Namespace
Codebelt.Bootstrapper
Assemblies
Codebelt.Bootstrapper.dll
Source
src/Codebelt.Bootstrapper/BootstrapperLifetime.cs

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

Name Description
BootstrapperLifetime(IOptions<ConsoleLifetimeOptions>, IHostEnvironment, IHostApplicationLifetime, IOptions<HostOptions>, ILoggerFactory)

Initializes a new instance of the BootstrapperLifetime class.

Properties

Name Description
OnApplicationStartedCallback

Triggered when the application host has fully started.

OnApplicationStoppedCallback

Triggered when the application host has completed a graceful shutdown.///

OnApplicationStoppingCallback

Triggered when the application host is starting a graceful shutdown.

Methods

Name Description
OnDisposeManagedResources()

Called when this object is being disposed by either Dispose() or Dispose(bool) having disposing set to true and Disposed is false.

StopAsync(CancellationToken)

Called from StopAsync(CancellationToken) to indicate that the host is stopping and it's time to shut down.

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.

See Also