コード例 #1
0
 private void LogPublishedRpcMessage(IMessage message,
                                     RpcMessagePublisher rpcPublisher,
                                     RpcProperties rpcProps)
 {
     _logger.LogTraceDetails(RabbitMqLogEvents.PUBLISHER_RPC_REQUEST, "Publishing to RPC Consumer",
                             new
     {
         Message = message,
         rpcPublisher.BrokerName,
         rpcPublisher.RequestQueueKey,
         rpcPublisher.RequestQueueName,
         rpcPublisher.Client.ReplyQueueName,
         rpcProps.ContentType,
         rpcProps.ExternalTypeName
     });
 }
コード例 #2
0
        /// <summary>
        /// Publishes a message to a consumer defined queue used for receiving
        /// RPC style messages.  The caller awaits the reply response message.
        /// </summary>
        /// <param name="message">The RPC style message to publish.</param>
        /// <returns>Future result after the reply is received.</returns>
        public async Task PublishToRpcConsumerAsync(IMessage message, CancellationToken cancellationToken)
        {
            Check.NotNull(message, nameof(message));

            AssertRpcCommand(message);

            var rpcCommandAttrib = message.GetAttribute <RpcCommandAttribute>();
            var command          = message as ICommand;

            RpcProperties rpcProps = new RpcProperties {
                ContentType      = rpcCommandAttrib.ContentType,
                ExternalTypeName = rpcCommandAttrib.ExternalTypeName
            };

            // Obtain the consumer queue on which the message should be published.
            RpcMessagePublisher rpcPublisher = GetRpcPublisher(rpcCommandAttrib);

            string[] orderedContentTypes =
            {
                message.GetContentType(),
                rpcProps.ContentType,
                rpcPublisher.ContentType
            };

            byte[] messageBody = _serializationMgr.Serialize(command, orderedContentTypes);

            LogPublishedRpcMessage(message, rpcPublisher, rpcProps);

            // Publish the RPC request the consumer's queue and await a response.
            rpcProps.ContentType = command.GetContentType();
            byte[] replyBody = null;

            try
            {
                replyBody = await rpcPublisher.Client.Invoke(command, rpcProps, cancellationToken, messageBody);

                object reply = _serializationMgr.Deserialize(rpcProps.ContentType, command.ResultType, replyBody);
                command.SetResult(reply);
                LogReceivedRpcResponse(message, rpcPublisher);
            }
            catch (RpcReplyException ex)
            {
                var dispatchEx = _serializationMgr.Deserialize <MessageDispatchException>(rpcProps.ContentType, ex.Exception);
                _logger.LogError(RabbitMqLogEvents.PUBLISHER_RPC_RESPONSE, "RPC Exception Reply.", dispatchEx);
                throw dispatchEx;
            }
        }