Skip to content

NeedlrBootstrapContext

NexusLabs.Needlr.Hosting

NeedlrBootstrapContext Class

Contextual data passed to the RunAsync(Func<NeedlrBootstrapContext,CancellationToken,Task>, CancellationToken) callback. Provides access to a bootstrap logger and a bootstrap Microsoft.Extensions.Configuration.IConfiguration that are available before the DI container is built.

public sealed record NeedlrBootstrapContext : System.IEquatable<NexusLabs.Needlr.Hosting.NeedlrBootstrapContext>

Inheritance System.Object 🡒 NeedlrBootstrapContext

Implements System.IEquatable<NeedlrBootstrapContext>

Example

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

        // Build the real app — Syringe creates its own IConfiguration independently.
        var host = new Syringe()
            .UsingSourceGen()
            .ForHost()
            .UsingOptions(() => CreateHostOptions.Default.UsingLogger(ctx.Logger))
            .BuildHost();
        await host.RunAsync(ct);
    });

Remarks

Everything on this context is bootstrap-phase only. These resources exist solely to bridge the gap until the DI container is built. Once Syringe (or any host builder) creates the real Microsoft.Extensions.Configuration.IConfiguration and logging pipeline, the bootstrap versions are disposed and should no longer be referenced.

In particular, BootstrapConfiguration is not the same Microsoft.Extensions.Configuration.IConfiguration that the application's DI container will provide. They are independent instances that may read different sources, have different precedence rules, or contain different values. Do not cache or leak the bootstrap configuration beyond the callback.

Properties

NeedlrBootstrapContext.BootstrapConfiguration Property

Gets a minimal Microsoft.Extensions.Configuration.IConfiguration built during the bootstrap phase, before the DI container exists.

public Microsoft.Extensions.Configuration.IConfiguration BootstrapConfiguration { get; init; }

Property Value

Microsoft.Extensions.Configuration.IConfiguration

Remarks

This configuration is for bootstrap-phase use only. It is not the same Microsoft.Extensions.Configuration.IConfiguration that will be available after the DI container is built. Syringe (and the .NET Generic Host / WebApplication builder) builds its own Microsoft.Extensions.Configuration.IConfiguration independently. The two may read the same files but are separate instances with no shared state.

By default the bootstrap configuration is empty. Use ConfigureBootstrapConfiguration(this NeedlrBootstrapper, Action<IConfigurationBuilder>) to add configuration sources (JSON files, environment variables, in-memory collections, etc.) that are needed during the bootstrap phase.

NeedlrBootstrapContext.Logger Property

Gets the bootstrap logger, available before the DI container is configured.

public Microsoft.Extensions.Logging.ILogger Logger { get; init; }

Property Value

Microsoft.Extensions.Logging.ILogger

Remarks

This logger is for bootstrap-phase diagnostics only. Once the DI container is built, the application's own ILogger<T> pipeline takes over.