ToolBX.AssemblyInitializer.Console 4.0.0

dotnet add package ToolBX.AssemblyInitializer.Console --version 4.0.0
                    
NuGet\Install-Package ToolBX.AssemblyInitializer.Console -Version 4.0.0
                    
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="ToolBX.AssemblyInitializer.Console" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ToolBX.AssemblyInitializer.Console" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ToolBX.AssemblyInitializer.Console" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ToolBX.AssemblyInitializer.Console --version 4.0.0
                    
#r "nuget: ToolBX.AssemblyInitializer.Console, 4.0.0"
                    
#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.
#:package ToolBX.AssemblyInitializer.Console@4.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ToolBX.AssemblyInitializer.Console&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ToolBX.AssemblyInitializer.Console&version=4.0.0
                    
Install as a Cake Tool

AssemblyInitializer

AssemblyInitializer

Helps decouple initialization logic by splitting it into AssemblyInitializer classes.

Good to know

This package includes the AutoInject library. It also automatically initializes AutoInject so you don't need to think about it at all. Just put those [AutoInject] attributes on the classes you want to inject and you're good to go.

For console applications, it also grants you the possibility to optionally use an appsettings.json file right out of the box.

Initializers are discovered at compile time by a source generator (not by scanning assemblies at runtime), so the library is trimming/NativeAOT-friendly.

Getting started

Put your initialization logic inside classes that implement IAssemblyInitializer. You're free to call them whatever you want. Each initializer must have a public parameterless constructor.

public class SomeAssemblyInitializer : IAssemblyInitializer
{
    public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        //This is where you would normally manually inject your services but you don't need to do that if you're using AutoInject

        //But if you really don't like AutoInject for some reason or can't use it because what you need to inject is from a third party
        services.AddSingleton<ISomeService, SomeService>();
    }

    public void Configure(IInitializerContext context)
    {
        //Runs after the service provider is built. Resolve whatever you need from context.Services.
        var configurator = context.Services.GetRequiredService<IConfigurator>();
        configurator.InitializeCurrentUserApplicationDataPath();
    }
}

IInitializerContext exposes the built IServiceProvider (Services), the IConfiguration (Configuration) and the IHostEnvironment (Environment). It replaces the old ASP.NET IApplicationBuilder/IHostEnvironment pair, so the core package no longer depends on the ASP.NET shared framework.

Setup

Console

Grab the ToolBX.AssemblyInitializer.Console package. Add a Startup class that inherits ConsoleStartup:

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

    public override void Run(IServiceProvider serviceProvider)
    {
        //Your application's entry logic.
    }
}

Then start it from Program.cs. Prefer the factory overload — it creates the startup with a plain constructor call, which is trimming/NativeAOT-safe:

ConsoleHost.UseStartup(configuration => new Startup(configuration), args);

There is also a convenience ConsoleHost.UseStartup<Startup>(args) overload that activates the startup via reflection. It is marked [RequiresUnreferencedCode]/[RequiresDynamicCode] and is not recommended when targeting NativeAOT.

Why

The goal of this library is to unify the way initialization is done across all application types. It also has the added advantage of decoupling initialization logic from the rest of your code or even from itself.

You can have as many AssemblyInitializers as you want so you could have one per concern if your initialization code is particularly crowded.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
4.0.0 34 7/18/2026
4.0.0-beta.1 64 6/14/2026
3.0.0 2,294 9/26/2024
3.0.0-beta2 177 9/23/2024
3.0.0-beta-1 193 2/7/2024
2.2.0 931 1/12/2024
2.2.0-beta5 211 1/7/2024
2.2.0-beta4 208 12/13/2023
2.2.0-beta2 201 11/16/2023
2.2.0-beta1 233 7/26/2023
2.0.2 284 6/19/2023
2.0.1 324 4/25/2023
2.0.0 477 11/11/2022
2.0.0-beta1 291 9/21/2022
1.0.1 532 9/21/2022
1.0.0 594 3/3/2022