85 lines
3.1 KiB
C#
85 lines
3.1 KiB
C#
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
{
|
|
using Autofac;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using RabbitMQ.Client;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public static class EventBusRabbitMqServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddEventBusRabbitMq(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddRabbitMQ(configuration);
|
|
|
|
var subscriptionClientName = configuration["SubscriptionClientName"];
|
|
|
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
|
|
{
|
|
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
|
|
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
|
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
|
|
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
|
|
|
var retryCount = 5;
|
|
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
|
|
{
|
|
retryCount = int.Parse(configuration["EventBusRetryCount"]);
|
|
}
|
|
|
|
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
|
|
});
|
|
|
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
|
|
|
return services;
|
|
}
|
|
|
|
private static IServiceCollection AddRabbitMQ(this IServiceCollection services, IConfiguration Configuration)
|
|
{
|
|
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|
{
|
|
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
|
|
|
|
|
var factory = new ConnectionFactory()
|
|
{
|
|
HostName = Configuration["EventBusConnection"],
|
|
DispatchConsumersAsync = true
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
|
{
|
|
factory.UserName = Configuration["EventBusUserName"];
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
|
{
|
|
factory.Password = Configuration["EventBusPassword"];
|
|
}
|
|
|
|
var retryCount = 5;
|
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
|
{
|
|
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
|
}
|
|
|
|
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|