public async Task PublishAsync <T>(T @event) where T : IntegrativeEvent
        {
            var channel  = _channelPool.Get();
            var exchange = typeof(T).GetEventExchange();

            try
            {
                channel.BasicPublish(
                    exchange: exchange,
                    routingKey: "",
                    basicProperties: null,
                    body: @event.Serialize());

                _logger.LogTrace("Event {Id} published on {Exchange}",
                                 @event.Id,
                                 exchange);

                await _storage.SaveAsync(@event, OperationType.Send, OperationStatus.Succeeded);
            }
            catch
            {
                _logger.LogError("Faild to publish event {Id} on {Exchange}",
                                 @event.Id,
                                 exchange);

                await _storage.SaveAsync(@event, OperationType.Send, OperationStatus.Failed);
            }

            _channelPool.Release(channel);
        }
Exemplo n.º 2
0
        public Task PublishAsync(IIntegrationEvent message)
        {
            var channel    = _channelPool.Get();
            var routingKey = message.GetType().Name;
            var body       = JsonSerializer.SerializeToUtf8Bytes(message, message.GetType());

            channel.ExchangeDeclare(
                exchange: ExchangeName,
                ExchangeType.Topic,
                durable: true,
                autoDelete: false);

            channel.BasicPublish(
                exchange: ExchangeName,
                routingKey: routingKey,
                basicProperties: null,
                body: body);

            _channelPool.Release(channel);

            return(Task.CompletedTask);
        }