Skip to content

NeedlrBootstrapperExtensions

NexusLabs.Needlr.Hosting

NeedlrBootstrapperExtensions Class

Extension methods for configuring NeedlrBootstrapper instances.

public static class NeedlrBootstrapperExtensions

Inheritance System.Object 🡒 NeedlrBootstrapperExtensions

Methods

NeedlrBootstrapperExtensions.ConfigureBootstrapConfiguration(this NeedlrBootstrapper, Action<IConfigurationBuilder>) Method

Configures the bootstrap-phase Microsoft.Extensions.Configuration.IConfiguration by adding sources to the Microsoft.Extensions.Configuration.IConfigurationBuilder.

public static NexusLabs.Needlr.Hosting.NeedlrBootstrapper ConfigureBootstrapConfiguration(this NexusLabs.Needlr.Hosting.NeedlrBootstrapper bootstrapper, System.Action<Microsoft.Extensions.Configuration.IConfigurationBuilder> configure);

Parameters

bootstrapper NeedlrBootstrapper

The bootstrapper to configure.

configure System.Action<Microsoft.Extensions.Configuration.IConfigurationBuilder>

A delegate that adds configuration sources to the Microsoft.Extensions.Configuration.IConfigurationBuilder.

Returns

NeedlrBootstrapper
A new NeedlrBootstrapper with the configuration builder registered.

Example

await new NeedlrBootstrapper()
    .ConfigureBootstrapConfiguration(builder => builder
        .AddJsonFile("appsettings.json", optional: true)
        .AddEnvironmentVariables())
    .RunAsync(async (ctx, ct) =>
    {
        var logDir = ctx.BootstrapConfiguration["Logging:Directory"]
            ?? "logs";
        ctx.Logger.LogInformation("Bootstrap log directory: {Dir}", logDir);
    });

Remarks

The bootstrap configuration is not the same Microsoft.Extensions.Configuration.IConfiguration that the application's DI container will provide. It exists only for the duration of the bootstrap callback and is disposed after RunAsync(Func<NeedlrBootstrapContext,CancellationToken,Task>, CancellationToken) completes. Syringe (and the .NET Generic Host / WebApplication builder) builds its own Microsoft.Extensions.Configuration.IConfiguration independently.

By default the bootstrap configuration is empty. Call this method to add JSON files, environment variables, in-memory collections, or any other Microsoft.Extensions.Configuration.IConfigurationSource needed during the bootstrap phase.

If called multiple times, only the last call takes effect.

NeedlrBootstrapperExtensions.UsingLoggerFactory(this NeedlrBootstrapper, ILoggerFactory) Method

Overrides the default console logger factory with a custom Microsoft.Extensions.Logging.ILoggerFactory.

public static NexusLabs.Needlr.Hosting.NeedlrBootstrapper UsingLoggerFactory(this NexusLabs.Needlr.Hosting.NeedlrBootstrapper bootstrapper, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory);

Parameters

bootstrapper NeedlrBootstrapper

The bootstrapper to configure.

loggerFactory Microsoft.Extensions.Logging.ILoggerFactory

The logger factory to use.

Returns

NeedlrBootstrapper
A new NeedlrBootstrapper with the factory applied.

Example

await new NeedlrBootstrapper()
    .UsingLoggerFactory(myLoggerFactory)
    .RunAsync(async (ctx, ct) => { /* ... */ });

NeedlrBootstrapperExtensions.WithCleanup(this NeedlrBootstrapper, Func<Task>) Method

Registers an async cleanup callback that runs in the finally block of RunAsync(Func<NeedlrBootstrapContext,CancellationToken,Task>, CancellationToken), regardless of whether the application succeeds or throws.

public static NexusLabs.Needlr.Hosting.NeedlrBootstrapper WithCleanup(this NexusLabs.Needlr.Hosting.NeedlrBootstrapper bootstrapper, System.Func<System.Threading.Tasks.Task> cleanup);

Parameters

bootstrapper NeedlrBootstrapper

The bootstrapper to configure.

cleanup System.Func<System.Threading.Tasks.Task>

The async cleanup delegate (e.g. flushing a log sink).

Returns

NeedlrBootstrapper
A new NeedlrBootstrapper with the cleanup registered.

Example

await new NeedlrBootstrapper()
    .WithCleanup(async () => await FlushLogsAsync())
    .RunAsync(async (ctx, ct) => { /* ... */ });