Skip to content

HostSyringeExtensions

NexusLabs.Needlr.Hosting

HostSyringeExtensions Class

Extension methods for configuring HostSyringe instances. Provides only host-specific configuration methods.

public static class HostSyringeExtensions

Inheritance System.Object 🡒 HostSyringeExtensions

Example

Complete host configuration:

var host = new Syringe()
    // Transition to host mode
    .ForHost()
    // Configure host-specific options
    .UsingOptions(() => CreateHostOptions.Default
        .UsingArgs(args)
        .UsingApplicationName("My Worker Service")
        .UsingStartupConsoleLogger())
    .UsingConfigurationCallback((builder, options) =>
    {
        // Configure the HostApplicationBuilder
        builder.Configuration.AddJsonFile("custom-settings.json", optional: true);
        builder.Services.AddSingleton<IMyCustomService, MyCustomService>();
    })
    .BuildHost();

await host.RunAsync();

Minimal host:

var host = new Syringe()
    .ForHost()
    .BuildHost();

Remarks

Remember to use base NexusLabs.Needlr.Injection.Syringe extension methods BEFORE transitioning to host mode.

Methods

HostSyringeExtensions.UsingConfigurationCallback(this HostSyringe, Action<HostApplicationBuilder,CreateHostOptions>) Method

Configures the HostSyringe to use a callback for configuring the Microsoft.Extensions.Hosting.HostApplicationBuilder. This allows for custom configuration of the builder, such as modifying the Microsoft.Extensions.Configuration.ConfigurationBuilder or adding additional services before the host is built.

public static NexusLabs.Needlr.Hosting.HostSyringe UsingConfigurationCallback(this NexusLabs.Needlr.Hosting.HostSyringe syringe, System.Action<Microsoft.Extensions.Hosting.HostApplicationBuilder,NexusLabs.Needlr.Hosting.CreateHostOptions> configureCallback);

Parameters

syringe HostSyringe

The HostSyringe to configure.

configureCallback System.Action<Microsoft.Extensions.Hosting.HostApplicationBuilder,CreateHostOptions>

The callback to configure the Microsoft.Extensions.Hosting.HostApplicationBuilder.

Returns

HostSyringe
A new configured HostSyringe instance.

Example

var hostSyringe = syringe
    .ForHost()
    .UsingConfigurationCallback((builder, options) =>
    {
        // Add custom configuration sources
        builder.Configuration.AddJsonFile("appsettings.local.json", optional: true);
        builder.Configuration.AddEnvironmentVariables("MYAPP_");

        // Register additional services that need to be available before plugin registration
        builder.Services.AddSingleton<ICustomConfigurationService, CustomConfigurationService>();

        // Configure logging
        builder.Services.AddLogging(logging =>
        {
            logging.AddConsole();
            logging.SetMinimumLevel(LogLevel.Debug);
        });
    });

HostSyringeExtensions.UsingHostFactory(this HostSyringe, Func<IServiceProviderBuilder,IServiceCollectionPopulator,IHostFactory>) Method

Configures the HostSyringe to use the specified host factory.

public static NexusLabs.Needlr.Hosting.HostSyringe UsingHostFactory(this NexusLabs.Needlr.Hosting.HostSyringe syringe, System.Func<NexusLabs.Needlr.Injection.IServiceProviderBuilder,NexusLabs.Needlr.Injection.IServiceCollectionPopulator,NexusLabs.Needlr.Hosting.IHostFactory> factory);

Parameters

syringe HostSyringe

The HostSyringe to configure.

factory System.Func<NexusLabs.Needlr.Injection.IServiceProviderBuilder,NexusLabs.Needlr.Injection.IServiceCollectionPopulator,IHostFactory>

The factory function for creating host factories.

Returns

HostSyringe
A new configured HostSyringe instance.

Example

var hostSyringe = syringe
    .ForHost()
    .UsingHostFactory((serviceProviderBuilder, serviceCollectionPopulator) => 
    {
        // Custom factory logic here
        return new CustomHostFactory(serviceProviderBuilder, serviceCollectionPopulator);
    });

HostSyringeExtensions.UsingOptions(this HostSyringe, ILogger) Method

Configures the HostSyringe to use Default with the specified logger for startup diagnostics. Use this when you already have an Microsoft.Extensions.Logging.ILogger instance and want to avoid the boilerplate of calling UsingOptions(() => CreateHostOptions.Default.UsingLogger(logger)).

public static NexusLabs.Needlr.Hosting.HostSyringe UsingOptions(this NexusLabs.Needlr.Hosting.HostSyringe syringe, Microsoft.Extensions.Logging.ILogger logger);

Parameters

syringe HostSyringe

The HostSyringe to configure.

logger Microsoft.Extensions.Logging.ILogger

The logger to use during startup.

Returns

HostSyringe
A new configured HostSyringe instance.

Exceptions

System.ArgumentNullException
Thrown when syringe or logger is null.

Example

var host = new Syringe()
    .UsingSourceGen()
    .ForHost()
    .UsingOptions(bootstrapLogger)
    .BuildHost();

HostSyringeExtensions.UsingOptions(this HostSyringe, Func<CreateHostOptions>) Method

Configures the HostSyringe to use the specified host options factory.

public static NexusLabs.Needlr.Hosting.HostSyringe UsingOptions(this NexusLabs.Needlr.Hosting.HostSyringe syringe, System.Func<NexusLabs.Needlr.Hosting.CreateHostOptions> optionsFactory);

Parameters

syringe HostSyringe

The HostSyringe to configure.

optionsFactory System.Func<CreateHostOptions>

The factory function for creating host options.

Returns

HostSyringe
A new configured HostSyringe instance.

Example

var hostSyringe = syringe
    .ForHost()
    .UsingOptions(() => CreateHostOptions.Default
        .UsingArgs(args)
        .UsingApplicationName("My App")
        .UsingStartupConsoleLogger());