public void DeserializeRequest(System.ServiceModel.Channels.Message message, object[] parameters)
        {
            object bodyFormatProperty;

            if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) ||
                (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw)
            {
                throw new InvalidOperationException("服务行为配置错误,请将WebHttpBinding的ContentTypeMapper属性设置为WfRawWebContentTypeMapper类型");
            }

            try
            {
                PerformanceMonitorHelper.GetDefaultMonitor().WriteExecutionDuration("DeserializeRequest", () =>
                {
                    string jsonStr = WcfUtils.GetMessageRawContent(message);

                    Dictionary <string, object> paramsInfo = JSONSerializerExecute.Deserialize <Dictionary <string, object> >(jsonStr);

                    Dictionary <string, object> headers = PopHeaderInfo(paramsInfo);
                    PushHeaderInfoToMessageProperties(message, headers);

                    Dictionary <string, string> connectionMappings = PopConnectionMappings(paramsInfo);
                    PushConnectionMappingsToMessageProperties(message, connectionMappings);

                    Dictionary <string, object> context = PopContextInfo(paramsInfo);
                    PushContextToMessageProperties(message, context);

                    GenericTicketTokenContainer container = PopGenericTicketTokenContainer(paramsInfo);
                    PushGenericTicketTokenContainer(message, container);

                    for (int i = 0; i < _OperationDesc.Messages[0].Body.Parts.Count; i++)
                    {
                        string paramName = _OperationDesc.Messages[0].Body.Parts[i].Name;
                        Type targetType  = this._OperationDesc.Messages[0].Body.Parts[i].Type;

                        object val = paramsInfo[paramName];

                        try
                        {
                            parameters[i] = JSONSerializerExecute.DeserializeObject(val, targetType);
                        }
                        catch (System.Exception ex)
                        {
                            string errorMessage = string.Format("反序列化参数{0}错误,类型为{1}:{2}",
                                                                paramName,
                                                                targetType.ToString(),
                                                                ex.Message);

                            throw new InvalidDataException(errorMessage, ex);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("传入的JSON格式错误:" + ex.Message, ex);
            }
        }
        /// <summary>
        /// 反序列化服务调用的返回结果
        /// </summary>
        /// <param name="message"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object DeserializeReply(System.ServiceModel.Channels.Message message, object[] parameters)
        {
            object bodyFormatProperty;

            if (message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) == false ||
                (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw)
            {
                throw new InvalidOperationException("服务行为配置错误,请将WebHttpBinding的ContentTypeMapper属性设置为WfRawWebContentTypeMapper类型");
            }

            try
            {
                string jsonStr = WcfUtils.GetMessageRawContent(message);

                return(JSONSerializerExecute.DeserializeObject(jsonStr, _OperationDesc.Messages[1].Body.ReturnValue.Type));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("传入的JSON格式错误:" + ex.Message, ex);
            }
        }
Exemple #3
0
        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            XmlDocument document = null;

            Message relayMessage = WcfUtils.SimpleCloneMessage(reply, out document);

            string jsonStr = WcfUtils.GetMessageRawContent(document);

            if (jsonStr.IsNotEmpty())
            {
                if (jsonStr.IndexOf("MCS.Library.WcfExtensions.WfErrorDTO") >= 0)
                {
                    WfErrorDTO error = JSONSerializerExecute.Deserialize <WfErrorDTO>(jsonStr);

                    throw new WfClientChannelException(error.Message)
                          {
                              Detail = error.Description
                          };
                }
            }

            reply = relayMessage;
        }