Skip to content

ConfigureHost

Configures .NET host options through dependency injection. The method sets HostOptions.ShutdownTimeout and HostOptions.BackgroundServiceExceptionBehavior from the provided values.

Use this method when a hosted application should have an explicit shutdown timeout or a consistent policy for background-service failures. For example, services that need time to flush queues or close external connections can use a longer shutdown timeout.

Usage

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

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

Parameters

shutdownTimeout: TimeSpan
Maximum amount of time the host should wait for graceful shutdown.

backgroundServiceExceptionBehavior: BackgroundServiceExceptionBehavior
Behavior used when a background service throws an unhandled exception.

Returns

The IServiceCollection instance with host options configured.

Type signature

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

All packages are released under the MIT License.