Skip to content

ConfigureHostOptions ​

Sets the two HostOptions values that matter most to a long-running service: how long shutdown may take, and what happens when a background service throws.

shutdownTimeout bounds the whole sequence rather than each service, so set it above the slowest StopAsync and below the supervisor's grace period, which is 10 seconds for docker stop and 30 for Kubernetes.

Usage ​

csharp
using Microsoft.Extensions.Hosting;
using AlmightyShogun.Hosting.ConsoleLifetime;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

builder.ConfigureHostOptions(
    TimeSpan.FromSeconds(30),
    BackgroundServiceExceptionBehavior.StopHost
);
csharp
using Microsoft.Extensions.Hosting;
using AlmightyShogun.Hosting.ConsoleLifetime;

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureHostOptions(
        TimeSpan.FromSeconds(30),
        BackgroundServiceExceptionBehavior.StopHost
    )
    .Build();
csharp
using Microsoft.Extensions.Hosting;
using AlmightyShogun.Hosting.ConsoleLifetime;
using Microsoft.Extensions.DependencyInjection;

builder.Services.ConfigureHostOptions(
    TimeSpan.FromSeconds(30),
    BackgroundServiceExceptionBehavior.StopHost
);

Parameters

shutdownTimeout: TimeSpan
Maximum time allowed for a graceful host shutdown before the host stops waiting.

backgroundServiceExceptionBehavior: BackgroundServiceExceptionBehavior
How the host responds when a background service throws an unhandled exception.

Returns

The same builder or service collection instance, with the host options configured.

Type signature ​

csharp
public IServiceCollection ConfigureHostOptions(
    TimeSpan shutdownTimeout,
    BackgroundServiceExceptionBehavior backgroundServiceExceptionBehavior
);

public IHostApplicationBuilder ConfigureHostOptions(
    TimeSpan shutdownTimeout,
    BackgroundServiceExceptionBehavior backgroundServiceExceptionBehavior
);

public IHostBuilder ConfigureHostOptions(
    TimeSpan shutdownTimeout,
    BackgroundServiceExceptionBehavior backgroundServiceExceptionBehavior
);

All packages are released under the MIT License.