86 lines
3.4 KiB
C#
86 lines
3.4 KiB
C#
using System;
|
|
using Svrnty.CQRS.Events.Abstractions.Streaming;
|
|
using Svrnty.CQRS.Events.RabbitMQ.Delivery;
|
|
using Svrnty.CQRS.Events.RabbitMQ.Configuration;
|
|
using Svrnty.CQRS.Events.Abstractions.Delivery;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Svrnty.CQRS.Events.Abstractions;
|
|
|
|
namespace Svrnty.CQRS.Events.RabbitMQ;
|
|
|
|
/// <summary>
|
|
/// Extension methods for registering RabbitMQ event delivery services.
|
|
/// </summary>
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Registers RabbitMQ as an external event delivery provider.
|
|
/// </summary>
|
|
/// <param name="services">The service collection.</param>
|
|
/// <param name="configure">Configuration action for RabbitMQ options.</param>
|
|
/// <returns>The service collection for method chaining.</returns>
|
|
/// <example>
|
|
/// <code>
|
|
/// services.AddRabbitMQEventDelivery(options =>
|
|
/// {
|
|
/// options.ConnectionString = "amqp://guest:guest@localhost:5672/";
|
|
/// options.ExchangePrefix = "myapp";
|
|
/// options.DefaultExchangeType = "topic";
|
|
/// options.EnablePublisherConfirms = true;
|
|
/// });
|
|
/// </code>
|
|
/// </example>
|
|
public static IServiceCollection AddRabbitMQEventDelivery(
|
|
this IServiceCollection services,
|
|
Action<RabbitMQConfiguration> configure)
|
|
{
|
|
if (services == null)
|
|
throw new ArgumentNullException(nameof(services));
|
|
if (configure == null)
|
|
throw new ArgumentNullException(nameof(configure));
|
|
|
|
// Configure options
|
|
services.Configure(configure);
|
|
|
|
// Register RabbitMQEventDeliveryProvider as both IEventDeliveryProvider and IExternalEventDeliveryProvider
|
|
services.TryAddSingleton<RabbitMQEventDeliveryProvider>();
|
|
services.AddSingleton<IEventDeliveryProvider>(sp => sp.GetRequiredService<RabbitMQEventDeliveryProvider>());
|
|
services.AddSingleton<IExternalEventDeliveryProvider>(sp => sp.GetRequiredService<RabbitMQEventDeliveryProvider>());
|
|
|
|
// Register as hosted service to ensure Start/StopAsync are called
|
|
services.AddHostedService<RabbitMQEventDeliveryHostedService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers RabbitMQ as an external event delivery provider with connection string.
|
|
/// </summary>
|
|
/// <param name="services">The service collection.</param>
|
|
/// <param name="connectionString">RabbitMQ connection string (amqp://...).</param>
|
|
/// <param name="configure">Optional additional configuration.</param>
|
|
/// <returns>The service collection for method chaining.</returns>
|
|
/// <example>
|
|
/// <code>
|
|
/// services.AddRabbitMQEventDelivery("amqp://guest:guest@localhost:5672/");
|
|
/// </code>
|
|
/// </example>
|
|
public static IServiceCollection AddRabbitMQEventDelivery(
|
|
this IServiceCollection services,
|
|
string connectionString,
|
|
Action<RabbitMQConfiguration>? configure = null)
|
|
{
|
|
if (services == null)
|
|
throw new ArgumentNullException(nameof(services));
|
|
if (string.IsNullOrWhiteSpace(connectionString))
|
|
throw new ArgumentException("Connection string cannot be null or whitespace.", nameof(connectionString));
|
|
|
|
return services.AddRabbitMQEventDelivery(options =>
|
|
{
|
|
options.ConnectionString = connectionString;
|
|
configure?.Invoke(options);
|
|
});
|
|
}
|
|
}
|