Skip to content

HostApplicationBuilderNeedlrExtensions

NexusLabs.Needlr.Hosting

HostApplicationBuilderNeedlrExtensions Class

Extension methods for integrating Needlr discovery into user-controlled Microsoft.Extensions.Hosting.HostApplicationBuilder instances. Use this when you want to maintain control over the host creation process while still benefiting from Needlr's automatic service discovery and plugin system.

public static class HostApplicationBuilderNeedlrExtensions

Inheritance System.Object 🡒 HostApplicationBuilderNeedlrExtensions

Example

Basic usage with user-controlled builder:

var builder = Host.CreateApplicationBuilder(args);

// Your existing configuration
builder.Services.AddMyCustomServices();

// Add Needlr discovery - runs IHostApplicationBuilderPlugin and IServiceCollectionPlugin
builder.UseNeedlrDiscovery();

// More of your configuration
builder.Services.AddOtherServices();

var host = builder.Build();

// Optionally run IHostPlugin plugins
host.RunHostPlugins();

await host.RunAsync();

Remarks

UseNeedlrDiscovery(this HostApplicationBuilder, ConfiguredSyringe, ILogger) provides a "reverse integration" approach where Needlr adapts to your builder rather than you adapting to Needlr. This is useful when: - You have existing host configuration code you want to preserve - You need fine-grained control over the builder lifecycle - You're integrating Needlr into a larger framework

Note: IHostPlugin plugins are NOT executed by UseNeedlrDiscovery(this HostApplicationBuilder, ConfiguredSyringe, ILogger) because the user controls when Microsoft.Extensions.Hosting.HostApplicationBuilder.Build is called. If you need IHostPlugin support, call RunHostPlugins(this IHost, ILogger) after building the host.

Methods

HostApplicationBuilderNeedlrExtensions.RunHostPlugins(this IHost, ILogger) Method

Runs IHostPlugin plugins on the built host. Call this after Microsoft.Extensions.Hosting.HostApplicationBuilder.Build if you need IHostPlugin support.

public static Microsoft.Extensions.Hosting.IHost RunHostPlugins(this Microsoft.Extensions.Hosting.IHost host, Microsoft.Extensions.Logging.ILogger? logger=null);

Parameters

host Microsoft.Extensions.Hosting.IHost

The built host to configure.

logger Microsoft.Extensions.Logging.ILogger

Optional logger for plugin execution logging.

Returns

Microsoft.Extensions.Hosting.IHost
The same Microsoft.Extensions.Hosting.IHost for method chaining.

Example

var builder = Host.CreateApplicationBuilder(args);
builder.UseNeedlrDiscovery();

var host = builder.Build();
host.RunHostPlugins(); // Run IHostPlugin plugins

await host.RunAsync();

Remarks

This method retrieves the plugin factory and candidate assemblies that were stored during UseNeedlrDiscovery(this HostApplicationBuilder, ConfiguredSyringe, ILogger) and uses them to run IHostPlugin plugins.

HostApplicationBuilderNeedlrExtensions.UseNeedlrDiscovery(this HostApplicationBuilder, ConfiguredSyringe, ILogger) Method

Integrates Needlr's automatic service discovery and plugin system into the Microsoft.Extensions.Hosting.HostApplicationBuilder.

public static Microsoft.Extensions.Hosting.HostApplicationBuilder UseNeedlrDiscovery(this Microsoft.Extensions.Hosting.HostApplicationBuilder builder, NexusLabs.Needlr.Injection.ConfiguredSyringe syringe, Microsoft.Extensions.Logging.ILogger? logger=null);

Parameters

builder Microsoft.Extensions.Hosting.HostApplicationBuilder

The host application builder to configure.

syringe NexusLabs.Needlr.Injection.ConfiguredSyringe

Optional syringe instance to use for configuration. If not provided, a default syringe is created. Use this to provide a pre-configured syringe with specific type registrars or filterers.

logger Microsoft.Extensions.Logging.ILogger

Optional logger for discovery and plugin execution logging.

Returns

Microsoft.Extensions.Hosting.HostApplicationBuilder
The same Microsoft.Extensions.Hosting.HostApplicationBuilder for method chaining.

Example

With a pre-configured syringe:

var syringe = new Syringe()
    .UsingReflection()
    .UsingAdditionalAssemblies(typeof(MyPluginAssembly).Assembly);

var builder = Host.CreateApplicationBuilder(args);
builder.UseNeedlrDiscovery(syringe);

Remarks

This method performs the following in order: 1. Discovers assemblies using the syringe's assembly provider 2. Runs all NexusLabs.Needlr.Hosting.IHostApplicationBuilderPlugin.Configure(NexusLabs.Needlr.Hosting.HostApplicationBuilderPluginOptions) methods 3. Runs all NexusLabs.Needlr.IServiceCollectionPlugin.Configure(NexusLabs.Needlr.ServiceCollectionPluginOptions) methods 4. Registers discovered types to the service collection

IHostPlugin plugins are NOT executed. If needed, call RunHostPlugins(this IHost, ILogger) after Microsoft.Extensions.Hosting.HostApplicationBuilder.Build.