Skip to content

WebApplicationSyringeExtensions

NexusLabs.Needlr.AspNet

WebApplicationSyringeExtensions Class

Extension methods for configuring WebApplicationSyringe instances. Provides only web application specific configuration methods.

public static class WebApplicationSyringeExtensions

Inheritance System.Object 🡒 WebApplicationSyringeExtensions

Example

Complete web application configuration:

var webApplication = new Syringe()
    // Transition to web application mode
    .ForWebApplication()
    // Configure web-specific options
    .UsingOptions(() => CreateWebApplicationOptions.Default
        .UsingCliArgs(args)
        .UsingApplicationName("My Web Application")
        .UsingStartupConsoleLogger())
    .UsingConfigurationCallback((builder, options) =>
    {
        // Configure the WebApplicationBuilder
        builder.Configuration.AddJsonFile("custom-settings.json", optional: true);
        builder.Services.AddSingleton<IMyCustomService, MyCustomService>();
    })
    .BuildWebApplication();

await webApplication.RunAsync();

Minimal web application:

var webApp = new Syringe()
    .ForWebApplication()
    .BuildWebApplication();

Remarks

Remember to use base NexusLabs.Needlr.Injection.Syringe extension methods BEFORE calling \<seealso cref="M:NexusLabs.Needlr.AspNet.SyringeAspNetExtensions.ForWebApplication(NexusLabs.Needlr.Injection.ConfiguredSyringe)" />.

Methods

WebApplicationSyringeExtensions.UsingConfigurationCallback(this WebApplicationSyringe, Action<WebApplicationBuilder,CreateWebApplicationOptions>) Method

Configures the WebApplicationSyringe to use a callback for configuring the Microsoft.AspNetCore.Builder.WebApplicationBuilder. This allows for custom configuration of the builder, such as modifying the Microsoft.Extensions.Configuration.ConfigurationBuilder or adding additional services before the web application is built.

public static NexusLabs.Needlr.AspNet.WebApplicationSyringe UsingConfigurationCallback(this NexusLabs.Needlr.AspNet.WebApplicationSyringe syringe, System.Action<Microsoft.AspNetCore.Builder.WebApplicationBuilder,NexusLabs.Needlr.AspNet.CreateWebApplicationOptions> configureCallback);

Parameters

syringe WebApplicationSyringe

The WebApplicationSyringe to configure.

configureCallback System.Action<Microsoft.AspNetCore.Builder.WebApplicationBuilder,CreateWebApplicationOptions>

The callback to configure the Microsoft.AspNetCore.Builder.WebApplicationBuilder.

Returns

WebApplicationSyringe
A new configured WebApplicationSyringe instance.

Example

var webAppSyringe = syringe
    .ForWebApplication()
    .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);
        });
    });

WebApplicationSyringeExtensions.UsingOptions(this WebApplicationSyringe, ILogger) Method

Configures the WebApplicationSyringe 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(() => CreateWebApplicationOptions.Default.UsingLogger(logger)).

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

Parameters

syringe WebApplicationSyringe

The WebApplicationSyringe to configure.

logger Microsoft.Extensions.Logging.ILogger

The logger to use during startup.

Returns

WebApplicationSyringe
A new configured WebApplicationSyringe instance.

Exceptions

System.ArgumentNullException
Thrown when syringe or logger is null.

Example

var webApp = new Syringe()
    .UsingSourceGen()
    .ForWebApplication()
    .UsingOptions(bootstrapLogger)
    .BuildWebApplication();

WebApplicationSyringeExtensions.UsingOptions(this WebApplicationSyringe, Func<CreateWebApplicationOptions>) Method

Configures the WebApplicationSyringe to use the specified web application options factory.

public static NexusLabs.Needlr.AspNet.WebApplicationSyringe UsingOptions(this NexusLabs.Needlr.AspNet.WebApplicationSyringe syringe, System.Func<NexusLabs.Needlr.AspNet.CreateWebApplicationOptions> optionsFactory);

Parameters

syringe WebApplicationSyringe

The WebApplicationSyringe to configure.

optionsFactory System.Func<CreateWebApplicationOptions>

The factory function for creating web application options.

Returns

WebApplicationSyringe
A new configured WebApplicationSyringe instance.

Example

var webAppSyringe = syringe
    .ForWebApplication()
    .UsingOptions(() => CreateWebApplicationOptions.Default
        .UsingCliArgs(args)
        .UsingApplicationName("My App")
        .UsingStartupConsoleLogger());

WebApplicationSyringeExtensions.UsingWebApplicationFactory(this WebApplicationSyringe, Func<IServiceProviderBuilder,IServiceCollectionPopulator,IWebApplicationFactory>) Method

Configures the WebApplicationSyringe to use the specified web application factory.

public static NexusLabs.Needlr.AspNet.WebApplicationSyringe UsingWebApplicationFactory(this NexusLabs.Needlr.AspNet.WebApplicationSyringe syringe, System.Func<NexusLabs.Needlr.Injection.IServiceProviderBuilder,NexusLabs.Needlr.Injection.IServiceCollectionPopulator,NexusLabs.Needlr.AspNet.IWebApplicationFactory> factory);

Parameters

syringe WebApplicationSyringe

The WebApplicationSyringe to configure.

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

The factory function for creating web application factories.

Returns

WebApplicationSyringe
A new configured WebApplicationSyringe instance.

Example

var webAppSyringe = syringe
    .ForWebApplication()
    .UsingWebApplicationFactory((serviceProviderBuilder, serviceCollectionPopulator) => 
    {
        // Custom factory logic here
        return new CustomWebApplicationFactory(serviceProviderBuilder, serviceCollectionPopulator);
    });