Path: redb/redb.Core/Services/RedbInitHostedService.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Services/RedbInitHostedService.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using redb.Core.Models.Configuration;
namespace redb.Core.Services;
///
/// Hosted service that initializes redb schema on startup.
/// Registered before BackgroundDeletionService to guarantee the schema
/// exists when other services start querying.
///
internal sealed class RedbInitHostedService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private readonly RedbServiceConfiguration _configuration;
private readonly ILogger? _logger;
public RedbInitHostedService(
IServiceProvider serviceProvider,
RedbServiceConfiguration configuration,
ILogger? logger = null)
{
_serviceProvider = serviceProvider;
_configuration = configuration;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
using var scope = _serviceProvider.CreateScope();
var redb = scope.ServiceProvider.GetService();
if (redb is null) return;
await redb.InitializeAsync(ensureCreated: _configuration.EnsureCreated);
_logger?.LogInformation("Redb initialized (EnsureCreated={EnsureCreated})", _configuration.EnsureCreated);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}