Example #1
0
        private IBasicProperties GetRequestProperties(ICommand command, RpcProperties rpcProps)
        {
            IBasicProperties props = this.Channel.CreateBasicProperties();

            props.Headers = new Dictionary <string, object> {
                { RPC_BROKER_NAME, _brokerName }
            };
            props.ReplyTo       = ReplyQueueName;
            props.CorrelationId = command.GetCorrelationId();
            props.ContentType   = rpcProps.ContentType;
            props.Type          = rpcProps.ExternalTypeName;

            return(props);
        }
Example #2
0
        public async Task <byte[]> Invoke(ICommand command, RpcProperties rpcProps,
                                          CancellationToken cancellationToken,
                                          byte[] messageBody)
        {
            Check.NotNull(command, nameof(command));
            Check.NotNull(rpcProps, nameof(rpcProps));
            Check.NotNull(messageBody, nameof(messageBody));

            // Associate a correlation value with the outgoing message.
            string correlationId = Guid.NewGuid().ToString();

            command.SetCorrelationId(correlationId);

            // Create a task that can be resolved in the future when the result
            // is received from the reply queue.
            var futureResult = new TaskCompletionSource <byte[]>();

            var rpcPendingRequest = new RpcPendingRequest(futureResult, cancellationToken,
                                                          _consumerSettings.CancelRequestAfterMs);

            _pendingRpcRequests[correlationId] = rpcPendingRequest;

            IBasicProperties basicProps = GetRequestProperties(command, rpcProps);

            Channel.BasicPublish(DEFAULT_EXCHANGE, _consumerSettings.RequestQueueName,
                                 basicProps,
                                 messageBody);

            try
            {
                return(await futureResult.Task);
            }
            catch (TaskCanceledException ex)
            {
                // If the pending request task has been canceled, remove the pending
                // request and unregister the cancellation delegate.
                if (_pendingRpcRequests.TryRemove(correlationId, out RpcPendingRequest pendingRequest))
                {
                    pendingRequest.UnRegister();
                }

                throw new BrokerException(
                          $"The RPC request with the correlation value of: {correlationId} was canceled. " +
                          $"The current timeout value is: {_consumerSettings.CancelRequestAfterMs} ms.",
                          ex);
            }
        }