Пример #1
0
 public MessageQueueRabbitMq(IConnection connection,
                             MessageQueueOptions options,
                             IMessageQueueInfo info)
 {
     _channel    = connection.CreateModel();
     _connection = connection;
     _options    = options;
     _info       = info;
 }
Пример #2
0
        public static IMessageQueueBuilder AddMessageQueueRabbitMq(this IServiceCollection services, Action <MessageQueueOptions> options, ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            services.Configure(options);
            var messageQueueOptions = new MessageQueueOptions();

            options?.Invoke(messageQueueOptions);

            services.TryAdd(new ServiceDescriptor(typeof(MessageQueueOptions), x => messageQueueOptions, serviceLifetime));
            services.TryAdd(new ServiceDescriptor(typeof(IMessageQueueRabbitMq), typeof(MessageQueueRabbitMq), serviceLifetime));
            services.TryAdd(new ServiceDescriptor(typeof(IMessageQueue), x => x.GetRequiredService <IMessageQueueRabbitMq>(), serviceLifetime));
            services.TryAdd(new ServiceDescriptor(typeof(IMessageQueueInfo), typeof(MessageQueueInfo), serviceLifetime));

            services.TryAdd(new ServiceDescriptor(typeof(IConnection), serviceProvider =>
            {
                var config  = serviceProvider.GetRequiredService <MessageQueueOptions>();
                var factory = new ConnectionFactory()
                {
                    HostName               = config.HostName,
                    Port                   = int.Parse(config.Port),
                    UserName               = config.UserName,
                    Password               = config.Password,
                    VirtualHost            = config.VirtualHost,
                    DispatchConsumersAsync = true
                };
                return(factory.CreateConnection());
            }, serviceLifetime));

            return(new MessageQueueBuilder(services));
        }
Пример #3
0
 public MessageQueueInfo(MessageQueueOptions options, IConfiguration configuration)
 {
     _options       = options;
     _configuration = configuration;
 }