WebApplicationSyringe
NexusLabs.Needlr.AspNet¶
WebApplicationSyringe Class¶
Provides a fluent API for configuring and building web applications using Needlr. Wraps a ConfiguredSyringe with additional web application functionality.
public sealed record WebApplicationSyringe : System.IEquatable<NexusLabs.Needlr.AspNet.WebApplicationSyringe>
Inheritance System.Object 🡒 WebApplicationSyringe
Implements System.IEquatable<WebApplicationSyringe>
Example¶
Creating and configuring a WebApplicationSyringe:
// Method 1: Transition from ConfiguredSyringe
var webAppSyringe = new Syringe()
.UsingReflection()
.ForWebApplication();
// Build and run the web application
var webApp = webAppSyringe
.UsingOptions(() => CreateWebApplicationOptions.Default.UsingCliArgs(args))
.UsingConfigurationCallback((builder, options) =>
{
// Configure the WebApplicationBuilder
builder.Configuration.AddJsonFile("custom-settings.json", optional: true);
builder.Services.AddSingleton<IMyCustomService, MyCustomService>();
})
.BuildWebApplication();
await webApp.RunAsync();
Constructors¶
WebApplicationSyringe(ConfiguredSyringe) Constructor¶
Initializes a new instance of the WebApplicationSyringe class.
Parameters¶
baseSyringe NexusLabs.Needlr.Injection.ConfiguredSyringe
The configured syringe to wrap.
Example¶
var configuredSyringe = new Syringe().UsingReflection();
var webAppSyringe = new WebApplicationSyringe(configuredSyringe);
Methods¶
WebApplicationSyringe.BuildServiceProvider(IConfiguration) Method¶
Builds a service provider with the configured settings.
public System.IServiceProvider BuildServiceProvider(Microsoft.Extensions.Configuration.IConfiguration config);
Parameters¶
config Microsoft.Extensions.Configuration.IConfiguration
The configuration to use for building the service provider.
Returns¶
System.IServiceProvider
The configured System.IServiceProvider.
Example¶
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var serviceProvider = new Syringe()
.UsingReflection()
.UsingScrutorTypeRegistrar()
.ForWebApplication()
.BuildServiceProvider(config);
var myService = serviceProvider.GetRequiredService<IMyService>();
WebApplicationSyringe.BuildWebApplication() Method¶
Builds a web application with the configured settings.
Returns¶
Microsoft.AspNetCore.Builder.WebApplication
The configured Microsoft.AspNetCore.Builder.WebApplication.
Example¶
var webApplication = new Syringe()
.UsingReflection()
.UsingScrutorTypeRegistrar()
.ForWebApplication()
.UsingOptions(() => CreateWebApplicationOptions.Default
.UsingCliArgs(args)
.UsingApplicationName("My Web App"))
.UsingConfigurationCallback((builder, options) =>
{
// Add custom configuration sources
builder.Configuration.AddJsonFile("appsettings.local.json", optional: true);
// Register additional services
builder.Services.AddSingleton<ICustomService, CustomService>();
})
.BuildWebApplication();
// Configure middleware and endpoints
webApplication.MapGet("/", () => "Hello World!");
await webApplication.RunAsync();