Ejemplo n.º 1
0
        public Task PublishAsync <TMessage>(TMessage message, Guid globalMessageId, PublishConfiguration config)
        {
            var context = _contextProvider.GetMessageContext(globalMessageId);
            var props   = _propertiesProvider.GetProperties <TMessage>(config.PropertyModifier + (p => p.Headers.Add(PropertyHeaders.Context, context)));

            Task exchangeTask;

            lock (_topologyLock)
            {
                exchangeTask = _topologyProvider.DeclareExchangeAsync(config.Exchange);
            }
            var channelTask = _channelFactory.GetChannelAsync();

            return(Task
                   .WhenAll(exchangeTask, channelTask)
                   .ContinueWith(t =>
            {
                lock (_publishLock)
                {
                    var ackTask = _acknowledger.GetAckTask(channelTask.Result);
                    channelTask.Result.BasicPublish(
                        exchange: config.Exchange.ExchangeName,
                        routingKey: _config.RouteWithGlobalId ? $"{config.RoutingKey}.{globalMessageId}" : config.RoutingKey,
                        basicProperties: props,
                        body: _serializer.Serialize(message)
                        );
                    return ackTask;
                }
            })
                   .Unwrap());
        }
        public Task PublishAsync <TMessage>(TMessage message, PublishConfiguration config)
        {
            var props       = _propertiesProvider.GetProperties <TMessage>(config.PropertyModifier);
            var messageId   = props.MessageId;
            var messageType = props.Headers[PropertyHeaders.MessageType];

            Task exchangeTask;

            lock (_topologyLock)
            {
                exchangeTask = _topologyProvider.DeclareExchangeAsync(config.Exchange);
            }
            var channelTask = _channelFactory.GetChannelAsync();

            return(Task
                   .WhenAll(exchangeTask, channelTask)
                   .ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    throw t.Exception;
                }

                channelTask.Result.BasicReturn += config.BasicReturn;

                lock (_publishLock)
                {
                    var ackTask = _acknowledger.GetAckTask(channelTask.Result);
                    channelTask.Result.BasicPublish(
                        exchange: config.Exchange.ExchangeName,
                        routingKey: config.RoutingKey,
                        basicProperties: props,
                        body: _serializer.Serialize(message),
                        mandatory: (config.BasicReturn != null)
                        );
                    _logger.LogDebug($"PublishMessageId:{messageId} MessageType:{messageType}");
                    return ackTask
                    .ContinueWith(a => {
                        channelTask.Result.BasicReturn -= config.BasicReturn;
                    });
                }
            })
                   .Unwrap());
        }