招聘API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
1.5 KiB

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
{
/// <summary>
/// RabbitMQ启动服务
/// </summary>
public static class RabbitMQExtension
{
public static void AddRabbitMQSetup(this IServiceCollection services, IHostEnvironment env)
{
RabbitMQConfig rabbitMQConfig = new ConfigHelper().Get<AppConfig>("appconfig", env.EnvironmentName).RabbitMQConfig;
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
{
ILogger<RabbitMQPersistentConnection> logger = sp.GetRequiredService<ILogger<RabbitMQPersistentConnection>>();
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);
});
}
}
}