Path: redb-route/redb.Route/Extensions/RouteHostedService.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/Extensions/RouteHostedService.cs
----
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using redb.Route.Core;
namespace redb.Route.Extensions;
///
/// integration for the route context.
/// Starts and stops routes with the application lifecycle.
/// Register via .
///
public sealed class RouteHostedService : IHostedService, IAsyncDisposable
{
private readonly RouteContext _context;
private readonly IEnumerable _configurators;
private readonly ILogger _logger;
/// Creates a new hosted service wrapping the route context.
/// Route context instance (resolved from DI).
/// Context configurators (route builders, components, inline routes).
/// Logger.
public RouteHostedService(
RouteContext context,
IEnumerable configurators,
ILogger logger)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_configurators = configurators ?? throw new ArgumentNullException(nameof(configurators));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
///
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting redb.Route hosted service...");
// Apply all DI-registered configurators (route builders, components, inline routes)
foreach (var configurator in _configurators)
{
configurator.Configure(_context);
}
await _context.Start(cancellationToken).ConfigureAwait(false);
_logger.LogInformation("redb.Route hosted service started with {Count} route(s).", _context.Routes.Count);
}
///
public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping redb.Route hosted service...");
await _context.Stop(cancellationToken).ConfigureAwait(false);
_logger.LogInformation("redb.Route hosted service stopped.");
}
///
public async ValueTask DisposeAsync()
{
await _context.DisposeAsync().ConfigureAwait(false);
}
}