Exemple #1
0
        private async Task <string> SubscribeToReplyAsync(CancellationToken cancellationToken)
        {
            if (_replyQueue != null)
            {
                return(_replyQueue);
            }

            using (await _replyQueueLock.AcquireAsync(cancellationToken))
            {
                if (_replyQueue != null)
                {
                    return(_replyQueue);
                }

                var replyQueue = $"toxon.micro.rpc.reply.{Guid.NewGuid()}";

                var exchange = await _bus.ExchangeDeclareAsync(ReplyExchangeName, ExchangeType.Direct);

                var queue = await _bus.QueueDeclareAsync(replyQueue);

                await _bus.BindAsync(exchange, queue, replyQueue);

                _bus.Consume(queue, (body, props, info) =>
                {
                    var correlationIdString = props.CorrelationId;

                    if (Guid.TryParse(correlationIdString, out var correlationId) && _responseHandlers.TryRemove(correlationId, out var tcs))
                    {
                        tcs.SetResult(MessageHelpers.FromArgs(body, props));
                    }
                });

                _replyQueue = replyQueue;
                return(_replyQueue);
            }
        }