using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using RabbitMQ.Client; using System; using Znyc.Recruitment.Common.Configs; using Znyc.Recruitment.Common.Helpers; using Znyc.Recruitment.Evenbus; namespace Znyc.Recruitment.Api.Extensions { /// /// RabbitMQ启动服务 /// public static class RabbitMQExtension { public static void AddRabbitMQSetup(this IServiceCollection services, IHostEnvironment env) { RabbitMQConfig rabbitMQConfig = new ConfigHelper().Get("appconfig", env.EnvironmentName).RabbitMQConfig; if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddSingleton(sp => { ILogger logger = sp.GetRequiredService>(); ConnectionFactory factory = new ConnectionFactory() { HostName = rabbitMQConfig.Connection, DispatchConsumersAsync = true }; factory.UserName = rabbitMQConfig.UserName; factory.Password = rabbitMQConfig.Password; int retryCount = rabbitMQConfig.RetryCount; return new RabbitMQPersistentConnection(factory, logger, retryCount); }); } } }