Path: redb-route/redb.Route.Http/Extensions/ServiceCollectionExtensions.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Http/Extensions/ServiceCollectionExtensions.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Extensions;
namespace redb.Route.Http;
///
/// Extension methods for registering the HTTP component with dependency injection.
///
public static class ServiceCollectionExtensions
{
///
/// Registers the HTTP and HTTPS components with the route context.
///
public static IServiceCollection AddRedbRouteHttp(this IServiceCollection services)
=> AddRedbRouteHttp(services, null);
///
/// Registers the HTTP and HTTPS components with global CORS configuration.
/// Example: services.AddRedbRouteHttp(cors => { cors.Enabled = true; cors.Origins = "https://example.com"; });
///
/// The service collection.
/// Optional action to configure global CORS defaults for all HTTP consumers.
public static IServiceCollection AddRedbRouteHttp(this IServiceCollection services, Action? configureCors)
{
// Shared server manager — single instance across all route contexts and HTTP-based connectors.
services.AddRedbRouteHttpHosting();
services.AddSingleton(sp =>
{
var component = new HttpComponent();
component.ServerManager = sp.GetRequiredService();
configureCors?.Invoke(component.DefaultCors);
return component;
});
services.AddSingleton(sp =>
{
var component = new HttpsComponent();
component.ServerManager = sp.GetRequiredService();
configureCors?.Invoke(component.DefaultCors);
return component;
});
// IRouteContextConfigurator is applied by RouteHostedService at startup --
// the correct registration hook (a lazy marker singleton never fires).
services.AddRouteContextConfigurator((sp, context) =>
{
context.AddComponent(sp.GetRequiredService());
context.AddComponent(sp.GetRequiredService());
});
return services;
}
}