Table of Contents

Interface IHostControl

Namespace
Invex.Extensions.Hosting.Control
Assembly
Invex.Extensions.Hosting.dll

Provides control over the host application lifetime, extending Microsoft.Extensions.Hosting.IHostApplicationLifetime with the ability to stop the application with a specific exit code.

[PublicAPI]
public interface IHostControl

Examples

public sealed class Worker(IHostControl hostControl)
{
    public void FailFast() => hostControl.StopApplication(exitCode: 1);
}

Remarks

Register this service by calling AddHostControl(IServiceCollection), then inject it in place of Microsoft.Extensions.Hosting.IHostApplicationLifetime wherever the extra control is needed. All members inherited from Microsoft.Extensions.Hosting.IHostApplicationLifetime delegate to the host's lifetime implementation, so existing lifetime behavior is unchanged.

Methods

StopApplication(int)

Requests termination of the current application and sets the process exit code.

void StopApplication(int exitCode = 0)

Parameters

exitCode int

The exit code that will be returned to the operating system when the application exits. By convention, 0 indicates success and any non-zero value indicates failure.

Remarks

This sets System.Environment.ExitCode to exitCode and then calls Microsoft.Extensions.Hosting.IHostApplicationLifetime.StopApplication() to begin a graceful shutdown: hosted services are stopped via Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken), and the Microsoft.Extensions.Hosting.IHostApplicationLifetime.ApplicationStopping and Microsoft.Extensions.Hosting.IHostApplicationLifetime.ApplicationStopped tokens fire as usual. Like Microsoft.Extensions.Hosting.IHostApplicationLifetime.StopApplication(), this method returns immediately; it does not wait for the shutdown to complete.