HostInitActions 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package HostInitActions --version 1.0.2
NuGet\Install-Package HostInitActions -Version 1.0.2
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="HostInitActions" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HostInitActions --version 1.0.2
#r "nuget: HostInitActions, 1.0.2"
#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 HostInitActions as a Cake Addin
#addin nuget:?package=HostInitActions&version=1.0.2

// Install HostInitActions as a Cake Tool
#tool nuget:?package=HostInitActions&version=1.0.2

Host init actions

This is a simple library for defining asynchronous operations to be performed before the application starts. Typically, it is an asynchronous initialization of singleton services registered in an IoC container. This means that there is no need to perform this initialization in a blocking manner before registering to the IoC container. HostInitActions are based on the IHostedService implementation, which means they only work in IHost implementation environments that support the IHostedService work flow. For example, within a regular WebHost in an ASP.NET Core application.

Registration of initialization actions

Registration of initialization actions is done in the ConfigureServices method of the host builder or Startup class.

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureServices(services =>
    services.AddSingleton<IDatabaseService, DatabaseService>();
        .AddInitAction<IDatabaseService>(async (databaseService, cancellationToken) =>
            await databaseService.CreateIfNotExistsAsync(cancellationToken);

First, it is necessary to register the initialization service using the AddAsyncServiceInitialization method which returns a collection for registering specific init actions that will be executed before the application starts.

The AddInitAction method expects the generic parameter of the service type that is required for initialization and also the function that executes the required init action on this service. The initialized service will be retrieved from the IoC Container and passed as a parameter to the initialization function along with the optional CancellationToken.

You can define multiple init actions and they will be executed sequentially in the order of registration.

        .AddInitAction<IDatabaseService>(async (databaseService, cancellationToken) =>
            await databaseService.CreateIfNotExistsAsync(cancellationToken);
        .AddInitAction<IDeviceClient>(async deviceClient =>
            await deviceClient.InitializeAsync();

It is also possible to define more services (max 5) for one init action in case you need to have better control over the execution of individual initializations.

        .AddInitAction<IDatabaseService, IDeviceClient>(async (databaseService, deviceClient) =>
            await deviceClient.PreInitializeAsync();
            await Task.WhenAll(new []
            await deviceClient.ConnectToDeviceAsync(port: 1223);

Invocation of init actions

Init actions are executed before:

  • The app's request processing pipeline is configured.
  • The server is started and IApplicationLifetime.ApplicationStarted is triggered.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
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
1.3.0 3,097 3/3/2024
1.2.0 9,367 2/25/2023
1.1.0 5,970 11/24/2022
1.0.3 350 11/22/2022
1.0.2 294 11/22/2022
1.0.1 416 11/10/2022
1.0.0 304 11/9/2022