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
<PackageReference Include="ToolBX.AssemblyInitializer.Console" Version="4.0.0" />
<PackageVersion Include="ToolBX.AssemblyInitializer.Console" Version="4.0.0" />
<PackageReference Include="ToolBX.AssemblyInitializer.Console" />
paket add ToolBX.AssemblyInitializer.Console --version 4.0.0
#r "nuget: ToolBX.AssemblyInitializer.Console, 4.0.0"
#:package ToolBX.AssemblyInitializer.Console@4.0.0
#addin nuget:?package=ToolBX.AssemblyInitializer.Console&version=4.0.0
#tool nuget:?package=ToolBX.AssemblyInitializer.Console&version=4.0.0
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 | Versions 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. |
-
net10.0
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 10.0.9)
- Microsoft.Extensions.Configuration.Json (>= 10.0.9)
- Microsoft.Extensions.DependencyInjection (>= 10.0.9)
- Microsoft.Extensions.Options (>= 10.0.9)
- ToolBX.AssemblyInitializer (>= 4.0.0)
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 |