Beispiel #1
0
 public void FillRemoteContext(RpcTransportMessageHeader messageHeader)
 {
     if (messageHeader != null)
     {
         foreach (var pair in messageHeader)
         {
             this[pair.Key] = pair.Value;
         }
     }
 }
        /// <summary>
        /// 响应远程请求
        /// </summary>
        public RpcTransportMessageResponse ProcessCommand(RpcTransportMessageRequest rpcRequest /*TODO: 请求上下文信息*/)
        {
            //本地服务发现
            ServiceCommandTypeInfo localCommandInfo = null;
            bool isLocalCommand = this.serviceBusRegistry.IsLocalServiceCommand(rpcRequest.ServiceUniqueName, out localCommandInfo);

            string responseMessageContent = null;

            if (isLocalCommand)
            {
                using (performanceCounter.BeginStopwatch(string.Format("localInvoke: {0}", rpcRequest.ServiceUniqueName.FullServiceUniqueName)))
                {
                    //本地服务
                    IServiceCommandResult commandResult = null;
                    var  commandData    = (IServiceCommand)this.serializer.DeserializeString(localCommandInfo.CommandType, rpcRequest.MessageContent);
                    bool triggeredLocal = serviceBus.triggerLocalCommand(localCommandInfo.CommandType, localCommandInfo.CommandResultType, commandData, out commandResult);
                    if (!triggeredLocal)
                    {
                        throw new WindServiceBusLocalServiceNotFoundException(rpcRequest.ServiceUniqueName.FullServiceUniqueName);
                    }

                    // 去除null判断,允许数据内容为null
                    //if (commandResult == null)
                    //{
                    //    throw new WindServiceBusException(string.Format("service command [{0}] process error!", rpcRequest.ServiceUniqueName.FullServiceUniqueName));
                    //}

                    responseMessageContent = this.serializer.SerializeString(commandResult);
                }
            }
            else
            {
                using (performanceCounter.BeginStopwatch(string.Format("remoteInvoke: {0}", rpcRequest.ServiceUniqueName.FullServiceUniqueName)))
                {
                    //远程服务
                    var resultWithContext = serviceBus.triggerRemoteCommand(rpcRequest.ServiceUniqueName, rpcRequest.MessageContent);
                    responseMessageContent = resultWithContext.ResponseMessageContent;
                }
            }

            //构建响应输出
            var messageHeader = new RpcTransportMessageHeader();
            var rpcResponse   = new RpcTransportMessageResponse(MessageIdGenerator.CreateMessageId())
            {
                MessageHeader        = messageHeader,
                CorrelationMessageId = rpcRequest.MessageId,
                ServiceUniqueName    = rpcRequest.ServiceUniqueName,
                MessageContent       = responseMessageContent
            };

            return(rpcResponse);
        }
        /// <summary>
        /// 响应远程广播请求
        /// </summary>
        /// <param name="rpcRequest"></param>
        /// <returns></returns>
        public RpcTransportMessageResponse ProcessBroadcastCommand(RpcTransportMessageRequest rpcRequest)
        {
            List <string> resultList = new List <string>();

            //1. 本地调用
            using (performanceCounter.BeginStopwatch(string.Format("localBroadcastInvoke: {0}", rpcRequest.ServiceUniqueName.FullServiceUniqueName)))
            {
                ServiceCommandTypeInfo localCommandInfo = null;
                bool isLocalCommand = this.serviceBusRegistry.IsLocalServiceCommand(rpcRequest.ServiceUniqueName, out localCommandInfo);

                if (isLocalCommand)
                {
                    IServiceCommandResult commandResult = null;
                    var  commandData    = (IServiceCommand)this.serializer.DeserializeString(localCommandInfo.CommandType, rpcRequest.MessageContent);
                    bool triggeredLocal = serviceBus.triggerLocalCommand(localCommandInfo.CommandType, localCommandInfo.CommandResultType, commandData, out commandResult);

                    if (triggeredLocal)
                    {
                        string responseMessageContent = this.serializer.SerializeString(commandResult);
                        resultList.Add(responseMessageContent);
                    }
                }
            }

            using (performanceCounter.BeginStopwatch(string.Format("remoteBroadcastInvoke: {0}", rpcRequest.ServiceUniqueName.FullServiceUniqueName)))
            {
                //2. 远程调用
                var resultWithContext = serviceBus.broadcastRemoteCommand(rpcRequest.ServiceUniqueName, rpcRequest.MessageContent);
                resultList.AddRange(resultWithContext.Select(r => r.ResponseMessageContent));
            }

            //3. 构建响应输出
            var contentResult = this.serializer.CombineToArray(resultList);

            var messageHeader = new RpcTransportMessageHeader();
            var rpcResponse   = new RpcTransportMessageResponse(MessageIdGenerator.CreateMessageId())
            {
                MessageHeader        = messageHeader,
                CorrelationMessageId = rpcRequest.MessageId,
                ServiceUniqueName    = rpcRequest.ServiceUniqueName,
                MessageContent       = contentResult
            };

            return(rpcResponse);
        }
Beispiel #4
0
 /// <summary>
 /// 使用MessageHeader填充
 /// </summary>
 /// <param name="messageHeader"></param>
 public RemoteServiceBusResponseContext(RpcTransportMessageHeader messageHeader)
 {
     this.FillRemoteContext(messageHeader);
 }
 private void fillRequestRemoteContext(CommandHeader header, RpcTransportMessageHeader rpcRequestHeader)
 {
     rpcRequestHeader["SourceAppClassId"] = header.AppClass.ToString();
     rpcRequestHeader["SourceCommandId"]  = header.CommandId.ToString();
 }