Path: redb-route/redb.Route.Firebase/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.Firebase/Extensions/ServiceCollectionExtensions.cs
----
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Extensions;
namespace redb.Route.Firebase;
///
/// Extension methods for registering Firebase components (FCM, Firestore, Storage) in DI.
///
public static class ServiceCollectionExtensions
{
///
/// Registers all Firebase components (fcm://, fstore://, fbstorage://) in the route context.
///
///
/// services.AddRedbRoute(route =>
/// {
/// route.Services.AddRedbRouteFirebase();
/// route.AddRouteBuilder<MyRoutes>();
/// });
///
///
///
public static IServiceCollection AddRedbRouteFirebase(
this IServiceCollection services,
Action? configure = null)
{
var options = new FirebaseOptions();
configure?.Invoke(options);
services.AddSingleton(sp =>
{
var provider = new FirebaseCredentialProvider
{
DefaultProjectId = options.ProjectId,
DefaultCredentialPath = options.CredentialPath,
};
// Fail fast on an unreadable credential file at registration time.
if (options.CredentialPath is not null)
provider.GetOrCreateApp(options.CredentialPath, options.ProjectId);
return provider;
});
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// IRouteContextConfigurator is applied by RouteHostedService at startup —
// the correct registration hook (a lazy marker singleton never fires).
services.AddRouteContextConfigurator((sp, context) =>
{
var credProvider = sp.GetRequiredService();
var fcm = sp.GetRequiredService();
fcm.CredentialProvider = credProvider;
context.AddComponent(fcm);
var firestore = sp.GetRequiredService();
firestore.CredentialProvider = credProvider;
context.AddComponent(firestore);
var storage = sp.GetRequiredService();
storage.CredentialProvider = credProvider;
context.AddComponent(storage);
});
return services;
}
}
///
/// Configuration options for the Firebase connector registration.
///
public sealed class FirebaseOptions
{
/// Path to the Firebase service-account JSON file.
public string? CredentialPath { get; set; }
/// Firebase/GCP project ID.
public string? ProjectId { get; set; }
}