ToolBX.AssemblyInitializer.Console 2.2.0

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

// Install ToolBX.AssemblyInitializer.Console as a Cake Tool
#tool nuget:?package=ToolBX.AssemblyInitializer.Console&version=2.2.0

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.

Getting started

Put your initialization logic inside AssemblyInitializer classes. You�re free to call them whatever you want so long as they implement the IAssemblyInitializer interface.

public class SomeAssemblyInitializer : IAssemblyInitializer
{
    public void Configure(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(IApplicationBuilder app, IHostEnvironment env)
    {
        //I need to do this in Rough Trigger to initialize some loading logic
        var configurator = app.ApplicationServices.GetRequiredService<IConfigurator>();
        configurator.InitializeCurrentUserApplicationDataPath();
    }
}

Setup

Web

All you need to do is for your Startup class to inherit from AutoStartup.

Console

There are a few more steps involved for Console applications. Be sure to grab the AssemblyInitializer.Console package instead of simply AssemblyInitializer.

First, you need to add a Startup class that inherits ConsoleStartup like this.

Then, in your Program.cs file you should call the ConsoleHost like this.

And you�re good to go!

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 net7.0 is compatible.  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. 
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-beta-1 71 2/7/2024
2.2.0 443 1/12/2024
2.2.0-beta5 96 1/7/2024
2.2.0-beta4 108 12/13/2023
2.2.0-beta2 91 11/16/2023
2.2.0-beta1 103 7/26/2023
2.0.2 160 6/19/2023
2.0.1 179 4/25/2023
2.0.0 316 11/11/2022
2.0.0-beta1 127 9/21/2022
1.0.1 368 9/21/2022
1.0.0 404 3/3/2022