⚠ Deprecated: Other
As I learned more about using the IHostBuilder/IHost API, I came to realize almost all of what I'd implemented in this assembly could be handled in the IHostBuilder/IHost system. Consequently, I'm deprecating my own assembly. J4JSoftware.DependencyInjection also contained an API for locating files. I've moved this to a new assembly, J4JSoftware.FileUtilities.
Suggested alternative: J4JSoftware.FileUtilities
provides customized IHostBuilder and IHost classes for simplified configuration
$ dotnet add package J4JSoftware.DependencyInjectionThere are breaking changes in this release. Please re-review the github documentation.
Provides a customized IHostBuilder-based system for producing an IHost instance which contain a number of useful services. These include:
Microsoft.AspNetCore.DataProtection)This assembly targets Net5 and has nullability enabled.
This example is derived from my GeoProcessor console app. However, it's been simplified to illustrate certain concepts more clearly and therefore will not work "as written".
internal class Program
{
internal static IHost? Host { get; private set; }
private static readonly CancellationToken _cancellationToken = new();
private static IJ4JLogger? _buildLogger;
private static async Task Main( string[] args )
{
var hostConfig = new J4JHostConfiguration()
.ApplicationName( "GeoProcessor" )
.Publisher( "J4JSoftware" )
.OperatingSystem( OSNames.Windows )
.AddApplicationConfigurationFile("appConfig.json")
.AddUserConfigurationFile("userConfig.json")
.AddConfigurationInitializers(SetupConfiguration)
.LoggerInitializer( SetupLogging )
.FilePathTrimmer(FilePathTrimmer)
.OptionsInitializer( SetupOptions )
.AddDependencyInjectionInitializers( SetupDependencyInjection )
.AddServicesInitializers( SetupServices );
_buildLogger = hostConfig.Logger;
var builder = hostConfig.CreateHostBuilder();
Host = builder.Build();
await Host!.RunAsync( _cancellationToken );
}
}
This example Main program shows the basic outline of how to use J4JConfigurationHost. You create an instance, call various extension methods on it, check to see if all the basic requirements are satisfied, create the builder by calling CreateHostBuilder(), make sure the builder got built, and then call Build() on the builder.
Please note, however, that we store the build-time logger, contained in J4JConfigurationHost's Logger property, in a field variable. This is done so we can reference it in some of the configuration actions we've defined below. That lets the configuration actions report on issues and problems when they execute when the IHost instance gets built.