using EntrustSettle.Common; using EntrustSettle.EventBus; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using RabbitMQ.Client; using System; namespace EntrustSettle.Extensions { /// /// Db 启动服务 /// public static class RabbitMQSetup { public static void AddRabbitMQSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); if (AppSettings.app(new string[] { "RabbitMQ", "Enabled" }).ObjToBool()) { services.AddSingleton(sp => { var logger = sp.GetRequiredService>(); var factory = new ConnectionFactory() { HostName = AppSettings.app(new string[] { "RabbitMQ", "Connection" }), DispatchConsumersAsync = true }; if (!string.IsNullOrEmpty(AppSettings.app(new string[] { "RabbitMQ", "UserName" }))) { factory.UserName = AppSettings.app(new string[] { "RabbitMQ", "UserName" }); } if (!string.IsNullOrEmpty(AppSettings.app(new string[] { "RabbitMQ", "Password" }))) { factory.Password = AppSettings.app(new string[] { "RabbitMQ", "Password" }); } if (!string.IsNullOrEmpty(AppSettings.app(new string[] { "RabbitMQ", "Port" }))) { factory.Port = AppSettings.app(new string[] { "RabbitMQ", "Port" }).ObjToInt(); } var retryCount = 5; if (!string.IsNullOrEmpty(AppSettings.app(new string[] { "RabbitMQ", "RetryCount" }))) { retryCount = AppSettings.app(new string[] { "RabbitMQ", "RetryCount" }).ObjToInt(); } return new RabbitMQPersistentConnection(factory, logger, retryCount); }); } } } }