Codebelt.Bootstrapper.Console 3.0.0-rc.1

This is a prerelease version of Codebelt.Bootstrapper.Console.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Codebelt.Bootstrapper.Console --version 3.0.0-rc.1                
NuGet\Install-Package Codebelt.Bootstrapper.Console -Version 3.0.0-rc.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Codebelt.Bootstrapper.Console" Version="3.0.0-rc.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Codebelt.Bootstrapper.Console --version 3.0.0-rc.1                
#r "nuget: Codebelt.Bootstrapper.Console, 3.0.0-rc.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Codebelt.Bootstrapper.Console as a Cake Addin
#addin nuget:?package=Codebelt.Bootstrapper.Console&version=3.0.0-rc.1&prerelease

// Install Codebelt.Bootstrapper.Console as a Cake Tool
#tool nuget:?package=Codebelt.Bootstrapper.Console&version=3.0.0-rc.1&prerelease                

About

An open-source family of assemblies (MIT license) that provide a uniform and consistent way of bootstraping your code with Program.cs paired with Startup.cs.

Also, common for all, is the implementation of the IHostedService interface; this means that all project types, including the traditional console, now have option for graceful shutdown should your application require this (cronjob scenarios or similar).

It is, by heart, free, flexible and built to extend and boost your agile codebelt.

Codebelt.Bootstrapper.Console

An implementation optimized for console applications.

CSharp Example

An example on how to use Codebelt.Bootstrapper.Console in C#:


// --- Program.cs ---

public class Program : ConsoleProgram<Startup>
{
    static async Task Main(string[] args)
    {
        await CreateHostBuilder(args)
            .Build()
            .RunAsync()
            .ConfigureAwait(false);
    }
}

// --- Startup.cs ---

public class Startup : ConsoleStartup
{
    public Startup(IConfiguration configuration, IHostEnvironment environment) : base(configuration, environment)
    {
    }

    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ICorrelationToken>(new CorrelationToken());
    }

    public override void ConfigureConsole(IServiceProvider serviceProvider)
    {
        var logger = serviceProvider.GetRequiredService<ILogger<Startup>>();

        BootstrapperLifetime.OnApplicationStartedCallback = () => logger.LogInformation("Started");
        BootstrapperLifetime.OnApplicationStoppingCallback = () =>
        {
            logger.LogWarning("Stopping and cleaning ..");
            Thread.Sleep(TimeSpan.FromSeconds(5)); // simulate graceful shutdown
            logger.LogWarning(".. done!");
        };
        BootstrapperLifetime.OnApplicationStoppedCallback = () => logger.LogCritical("Stopped");
    }

    public async override Task RunAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
    {
        var logger = serviceProvider.GetRequiredService<ILogger<Startup>>();

        logger.LogInformation("Guid: {Guid}", serviceProvider.GetRequiredService<ICorrelationToken>());

        for (int dots = 0; dots <= 5; ++dots)
        {
            if (cancellationToken.IsCancellationRequested) { return; }
            System.Console.Write($"\rFire and forget {Generate.FixedString('.', dots)}");
            await Task.Delay(500, cancellationToken).ConfigureAwait(false);
        }

        System.Console.WriteLine("\nDone and done!");
    }
}

And the minimal equivalent example on how to use Codebelt.Bootstrapper.Console in C#:


// --- Program.cs ---

public class Program : MinimalConsoleProgram
{
    static Task Main(string[] args)
    {
        var builder = CreateHostBuilder(args);

        builder.Services.AddSingleton<ICorrelationToken>(new CorrelationToken());

        var host = builder.Build();

        var logger = host.Services.GetRequiredService<ILogger<Program>>();
        BootstrapperLifetime.OnApplicationStartedCallback = () => logger.LogWarning("Console started.");
        BootstrapperLifetime.OnApplicationStoppingCallback = () =>
        {
            logger.LogWarning("Stopping and cleaning ..");
            Thread.Sleep(TimeSpan.FromSeconds(5)); // simulate graceful shutdown
            logger.LogWarning(".. done!");
        };
        BootstrapperLifetime.OnApplicationStoppedCallback = () => logger.LogCritical("Console stopped.");

        return host.RunAsync();
    }

    public async override Task RunAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
    {
        var logger = serviceProvider.GetRequiredService<ILogger<Program>>();

        logger.LogInformation("Guid: {Guid}", serviceProvider.GetRequiredService<ICorrelationToken>());

        for (int dots = 0; dots <= 5; ++dots)
        {
            if (cancellationToken.IsCancellationRequested) { return; }
            System.Console.Write($"\rFire and forget {Generate.FixedString('.', dots)}");
            await Task.Delay(500, cancellationToken).ConfigureAwait(false);
        }

        System.Console.WriteLine("\nDone and done!");
    }
}

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 63 11/13/2024
3.0.0-rc.1 44 11/11/2024
3.0.0-preview.2 48 10/13/2024
3.0.0-preview.1 47 10/7/2024
2.0.0 98 9/8/2024
1.3.0 157 2/11/2024
1.2.0 184 12/15/2023
1.1.0 608 11/9/2022
1.0.1 294 12/29/2021
1.0.0 417 5/7/2021

Version 3.0.0
Availability: .NET 9 and .NET 8
 
# ALM
- CHANGED Dependencies to latest and greatest with respect to TFMs
- REMOVED Support for TFM .NET 6 (LTS)
 
# Breaking Changes
- REMOVED Run (abstract method) from the ConsoleStartup class located in the Codebelt.Bootstrapper.Console namespace
- REMOVED ILogger{TStartup} argument from the ConsoleHostedService class in the Codebelt.Bootstrapper.Console namespace
 
# New Features
- ADDED RunAsync (abstract method) to the ConsoleStartup class in the Codebelt.Bootstrapper.Console namespace
- ADDED ConfigureConsole (virtual method) to the ConsoleStartup class in the Codebelt.Bootstrapper.Console namespace
- ADDED HostApplicationBuilderExtensions class in the Codebelt.Bootstrapper.Console namespace that consist of extension methods for the HostApplicationBuilder class: UseBootstrapperProgram and UseMinimalConsoleProgram
- ADDED IProgramFactory interface in the Codebelt.Bootstrapper.Console namespace that provides an interface for initializing services and middleware used by an application
- ADDED MinimalConsoleHostedService class in the Codebelt.Bootstrapper.Console namespace that provides a console application that is managed by its host
- ADDED MinimalConsoleProgram class in the Codebelt.Bootstrapper.Console namespace that provides the base entry point of an application optimized for console applications
- ADDED ProgramFactory class in the Codebelt.Bootstrapper.Console namespace that is the default implementation of IProgramFactory
 
# Improvements
- CHANGED ConsoleHostedService class in the Codebelt.Bootstrapper.Console namespace to provide a significantly better developer experience
 
Version: 2.0.0
Availability: .NET 6.0 and .NET 8.0
 
# ALM
- REMOVED TFM for net7.0
- CHANGED Dependencies to latest and greatest with respect to TFMs
 
Version: 1.3.0
Availability: .NET 6.0, .NET 7.0, .NET 8.0
 
# ALM
- CHANGED Dependencies to latest and greatest
 
Version: 1.2.0
Availability: .NET 6.0, .NET 7.0, .NET 8.0
 
# ALM
- ADDED TFM for net8.0
- CHANGED Dependencies to latest and greatest with respect to TFMs
 
Version: 1.1.0
Availability: .NET 6.0, .NET 7.0
 
# ALM
- ADDED TFM for .NET 7
- CHANGED Dependencies to latest and greatest with respect to TFM
 
Version: 1.0.1
Availability: .NET 5.0, .NET 6.0
 
# ALM
- ADDED TFM for .NET 6
- CHANGED Dependencies to latest and greatest with respect to TFM
 
# Breaking Changes
- CHANGED namespace from Codebelt.Bootstrapper.Common to Codebelt.Bootstrapper.Console
 
# Bug Fixes
- FIXED non-critical bug in the ConsoleHostedService class located in the Codebelt.Bootstrapper.Console namespace
 
Version: 1.0.0
Availability: .NET 5.0
 
# New Features
- ADDED ConsoleHostedService class in the Codebelt.Bootstrapper.Common namespace that provides a console application that is managed by its host
- ADDED ConsoleProgram class in the Codebelt.Bootstrapper.Common namespace that is the base entry point of an application responsible for registering its ConsoleStartup partner
- ADDED ConsoleStartup interface in the Codebelt.Bootstrapper.Common namespace that provides the base class of a conventional based Startup class for a console application
- ADDED HostBuilderExtensions class in the Codebelt.Bootstrapper.Common namespace that consist of extension methods for the IHostBuilder interface: UseConsoleStartup