61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using System;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Svrnty.CQRS.Configuration;
|
|
using Svrnty.CQRS.Sagas.Abstractions.Messaging;
|
|
|
|
namespace Svrnty.CQRS.Sagas.RabbitMQ;
|
|
|
|
/// <summary>
|
|
/// Extensions for adding RabbitMQ saga transport to the CQRS pipeline.
|
|
/// </summary>
|
|
public static class CqrsBuilderExtensions
|
|
{
|
|
/// <summary>
|
|
/// Uses RabbitMQ as the message transport for sagas.
|
|
/// </summary>
|
|
/// <param name="builder">The CQRS builder.</param>
|
|
/// <param name="configure">Configuration action for RabbitMQ options.</param>
|
|
/// <returns>The CQRS builder for chaining.</returns>
|
|
public static CqrsBuilder UseRabbitMq(this CqrsBuilder builder, Action<RabbitMqSagaOptions> configure)
|
|
{
|
|
var options = new RabbitMqSagaOptions();
|
|
configure(options);
|
|
|
|
builder.Services.Configure<RabbitMqSagaOptions>(opt =>
|
|
{
|
|
opt.HostName = options.HostName;
|
|
opt.Port = options.Port;
|
|
opt.UserName = options.UserName;
|
|
opt.Password = options.Password;
|
|
opt.VirtualHost = options.VirtualHost;
|
|
opt.CommandExchange = options.CommandExchange;
|
|
opt.ResponseExchange = options.ResponseExchange;
|
|
opt.QueuePrefix = options.QueuePrefix;
|
|
opt.DurableQueues = options.DurableQueues;
|
|
opt.PrefetchCount = options.PrefetchCount;
|
|
opt.ConnectionRetryDelay = options.ConnectionRetryDelay;
|
|
opt.MaxConnectionRetries = options.MaxConnectionRetries;
|
|
});
|
|
|
|
// Replace the default message bus with RabbitMQ implementation
|
|
builder.Services.RemoveAll<ISagaMessageBus>();
|
|
builder.Services.AddSingleton<ISagaMessageBus, RabbitMqSagaMessageBus>();
|
|
|
|
// Add hosted service for connection management
|
|
builder.Services.AddHostedService<RabbitMqSagaHostedService>();
|
|
|
|
return builder;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uses RabbitMQ as the message transport for sagas with default options.
|
|
/// </summary>
|
|
/// <param name="builder">The CQRS builder.</param>
|
|
/// <returns>The CQRS builder for chaining.</returns>
|
|
public static CqrsBuilder UseRabbitMq(this CqrsBuilder builder)
|
|
{
|
|
return builder.UseRabbitMq(_ => { });
|
|
}
|
|
}
|