using System; using Autofac; using EntrustSettle.Common; using EntrustSettle.EventBus; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace EntrustSettle.Extensions { /// /// EventBus 事件总线服务 /// public static class EventBusSetup { public static void AddEventBusSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); if (AppSettings.app(new string[] { "EventBus", "Enabled" }).ObjToBool()) { var subscriptionClientName = AppSettings.app(new string[] { "EventBus", "SubscriptionClientName" }); services.AddSingleton(); services.AddTransient(); if (AppSettings.app(new string[] { "RabbitMQ", "Enabled" }).ObjToBool()) { services.AddSingleton(sp => { var rabbitMQPersistentConnection = sp.GetRequiredService(); var iLifetimeScope = sp.GetRequiredService(); var logger = sp.GetRequiredService>(); var eventBusSubcriptionsManager = sp.GetRequiredService(); var retryCount = 5; if (!string.IsNullOrEmpty(AppSettings.app(new string[] { "RabbitMQ", "RetryCount" }))) { retryCount = int.Parse(AppSettings.app(new string[] { "RabbitMQ", "RetryCount" })); } return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount); }); } if (AppSettings.app(new string[] { "Kafka", "Enabled" }).ObjToBool()) { services.AddHostedService(); services.AddSingleton(); } } } } }