Table of Contents

Class HostBuilderExtensions

Namespace
Codebelt.Bootstrapper
Assembly
Codebelt.Bootstrapper.dll

Extension methods for the IHostBuilder.

public static class HostBuilderExtensions
Inheritance
HostBuilderExtensions

Examples

The following example shows how to install BootstrapperLifetime, register a conventional StartupRoot partner through UseBootstrapperStartup<TStartup>, and apply local-development environment defaults on a conventional IHostBuilder produced by Host.CreateDefaultBuilder.

using Codebelt.Bootstrapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace HostBuilderExtensionsDemo;

public class MyStartup : StartupRoot
{
    public MyStartup(IConfiguration configuration, IHostEnvironment environment) : base(configuration, environment)
    {
    }

    public override void ConfigureServices(IServiceCollection services)
    {
    }
}

public static class Program
{
    public static void Main()
    {
        var host = Host.CreateDefaultBuilder()
            .UseBootstrapperLifetime()
            .UseBootstrapperStartup<MyStartup>()
            .UseBootstrapperEnvironmentDefaults<MyStartup>()
            .Build();

        var lifetime = host.Services.GetRequiredService<IHostLifetime>();
        var factory = host.Services.GetRequiredService<IStartupFactory<MyStartup>>();
        host.Run();
    }
}

Methods

UseBootstrapperEnvironmentDefaults<TStartup>(IHostBuilder)

Adds conventional environment defaults for local development.

public static IHostBuilder UseBootstrapperEnvironmentDefaults<TStartup>(this IHostBuilder hostBuilder) where TStartup : StartupRoot

Parameters

hostBuilder IHostBuilder

The IHostBuilder to configure.

Returns

IHostBuilder

The same instance of the IHostBuilder for chaining.

Type Parameters

TStartup

The type of the startup class used to resolve user secrets.

Remarks

When the current environment is local development, this method adds user secrets for TStartup to the application configuration.

UseBootstrapperLifetime(IHostBuilder)

Listens for Ctrl+C or SIGTERM and calls StopApplication() to start the shutdown process.

public static IHostBuilder UseBootstrapperLifetime(this IHostBuilder hostBuilder)

Parameters

hostBuilder IHostBuilder

The IHostBuilder to configure.

Returns

IHostBuilder

The same instance of the IHostBuilder for chaining.

Remarks

Complements the default implementation of ConsoleLifetime.

UseBootstrapperStartup<TStartup>(IHostBuilder)

Provides an implementation of a conventional based IStartupFactory<TStartup>.

public static IHostBuilder UseBootstrapperStartup<TStartup>(this IHostBuilder hostBuilder) where TStartup : StartupRoot

Parameters

hostBuilder IHostBuilder

The IHostBuilder to configure.

Returns

IHostBuilder

The same instance of the IHostBuilder for chaining.

Type Parameters

TStartup