using System; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace Svrnty.CQRS.Events.SignalR; /// /// Extension methods for registering SignalR event streaming services. /// public static class ServiceCollectionExtensions { /// /// Adds SignalR-based real-time event streaming to the service collection. /// /// The service collection. /// The service collection for method chaining. /// /// /// This registers the EventStreamHub and enables real-time event push to browser clients. /// Clients can connect via SignalR and subscribe to event streams. /// /// /// Prerequisites: /// - Call services.AddSignalR() first to register SignalR core services /// - Event streaming must be configured via AddSvrntyEvents() /// /// /// /// /// var builder = WebApplication.CreateBuilder(args); /// /// // Register SignalR and event streaming /// builder.Services.AddSignalR(); /// builder.Services.AddSvrntyEvents(); /// builder.Services.AddEventStreamHub(); // Enable SignalR event streaming /// /// var app = builder.Build(); /// /// // Map the hub endpoint /// app.MapHub<EventStreamHub>("/hubs/events"); /// /// app.Run(); /// /// public static IServiceCollection AddEventStreamHub(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); // EventStreamHub is automatically registered by SignalR when mapped // No explicit registration needed here return services; } /// /// Maps the EventStreamHub to the specified endpoint. /// /// The web application. /// The URL pattern for the hub endpoint (default: "/hubs/events"). /// The web application for method chaining. /// /// /// app.MapEventStreamHub(); // Maps to /hubs/events /// app.MapEventStreamHub("/api/events"); // Custom endpoint /// /// public static WebApplication MapEventStreamHub( this WebApplication app, string pattern = "/hubs/events") { if (app == null) throw new ArgumentNullException(nameof(app)); if (string.IsNullOrWhiteSpace(pattern)) throw new ArgumentException("Pattern cannot be null or empty", nameof(pattern)); app.MapHub(pattern); return app; } /// /// Adds persistent subscription support via SignalR to the service collection. /// /// The service collection. /// The service collection for method chaining. /// /// /// This registers the PersistentSubscriptionHub for correlation-based event subscriptions /// that persist across disconnections and support selective event filtering. /// /// /// Prerequisites: /// - Call services.AddSignalR() first to register SignalR core services /// - Call services.AddPersistentSubscriptions() to register subscription infrastructure /// - Event streaming must be configured via AddSvrntyEvents() /// /// /// /// /// var builder = WebApplication.CreateBuilder(args); /// /// // Register services /// builder.Services.AddSignalR(); /// builder.Services.AddSvrntyEvents(); /// builder.Services.AddPersistentSubscriptions(); /// builder.Services.AddPersistentSubscriptionHub(); /// /// var app = builder.Build(); /// /// // Map the hub endpoint /// app.MapPersistentSubscriptionHub("/hubs/subscriptions"); /// /// app.Run(); /// /// public static IServiceCollection AddPersistentSubscriptionHub(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); // PersistentSubscriptionHub is automatically registered by SignalR when mapped // No explicit registration needed here return services; } /// /// Maps the PersistentSubscriptionHub to the specified endpoint. /// /// The web application. /// The URL pattern for the hub endpoint (default: "/hubs/subscriptions"). /// The web application for method chaining. /// /// /// app.MapPersistentSubscriptionHub(); // Maps to /hubs/subscriptions /// app.MapPersistentSubscriptionHub("/api/subscriptions"); // Custom endpoint /// /// public static WebApplication MapPersistentSubscriptionHub( this WebApplication app, string pattern = "/hubs/subscriptions") { if (app == null) throw new ArgumentNullException(nameof(app)); if (string.IsNullOrWhiteSpace(pattern)) throw new ArgumentException("Pattern cannot be null or empty", nameof(pattern)); app.MapHub(pattern); return app; } }