public async Task SendAsync<TEvent>(IEventNotification<TEvent> context, CancellationToken cancellationToken = default) where TEvent : IEvent
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));

            var message = _messageFactory.CreateMessage(context);

            using (var connection = await _connectionFactory.CreateConnectionAsync(cancellationToken))
            {
                await connection.PublishAsync(message, cancellationToken);
            }
        }
 public async Task CreateExchangeAsync(string name, string exchangeType, bool durable = true, bool autoDelete = false)
 {
     using (var connection = await _connectionFactory.CreateConnectionAsync(CancellationToken.None))
     {
         await connection.CreateExchangeAsync(name, exchangeType, durable, autoDelete);
     }
 }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var connection =
                await _rabbitMqConnectionFactory.CreateConnectionAsync(_rabbitMqConfiguration.Uri, cancellationToken);

            await connection.WithModelAsync(model =>
            {
                model.ExchangeDeclare(_rabbitMqConfiguration.Exchange, ExchangeType.Fanout);
                model.QueueDeclare(_environmentConfiguration.RabbitQueueName, false, false, true, null);
                model.QueueBind(_environmentConfiguration.RabbitQueueName, _rabbitMqConfiguration.Exchange, "");

                var consume       = new EventingBasicConsumer(model);
                consume.Received += (obj, @event) =>
                {
                    var msg         = CreateRabbitMqMessage(@event);
                    var domainEvent = _eventJsonSerializer.Deserialize(msg.Message, new Metadata(msg.Headers));

                    _dispatchToEventSubscribers.DispatchToAsynchronousSubscribersAsync(domainEvent, cancellationToken);
                };


                model.BasicConsume(_environmentConfiguration.RabbitQueueName, false, consume);
                return(Task.CompletedTask);
            }, cancellationToken);
        }
Exemplo n.º 4
0
        private async Task <IRabbitConnection> GetRabbitMqConnectionAsync(Uri uri, CancellationToken cancellationToken)
        {
            using (await _asyncLock.WaitAsync(cancellationToken).ConfigureAwait(false))
            {
                IRabbitConnection rabbitConnection;
                if (_connections.TryGetValue(uri, out rabbitConnection))
                {
                    return(rabbitConnection);
                }

                rabbitConnection = await _connectionFactory.CreateConnectionAsync(uri, cancellationToken).ConfigureAwait(false);

                _connections.Add(uri, rabbitConnection);

                return(rabbitConnection);
            }
        }
        private async Task ExecuteAsync(RabbitMqSubscription subscription, CancellationToken cancellationToken)
        {
            using (var connection = await _connectionFactory.CreateConnectionAsync(cancellationToken))
            {
                var underlyingConnection = connection.UnderlyingConnection;
                var channel  = underlyingConnection.CreateModel();
                var consumer = new AsyncEventingBasicConsumer(channel);
                channel.BasicConsume(consumer: consumer, queue: subscription.Name, autoAck: false);

                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        var message = await ReceiveAsync(consumer);

                        try
                        {
                            await OnReceiveAsync(message, cancellationToken).ConfigureAwait(false);

                            channel.BasicAck(message.DeliveryTag, multiple: false);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, $"Error: RabbitMQ Message Receiver error, could not consume message '{message.DeliveryTag}'");

                            channel.BasicNack(message.DeliveryTag, multiple: false, requeue: true);

                            await OnErrorAsync(message, ex).ConfigureAwait(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, $"Unexpected receiver error: {ex.Message}");
                        throw;
                    }
                }
                foreach (var tag in consumer.ConsumerTags)
                {
                    channel.BasicCancel(tag);
                }
            }
        }