Esempio n. 1
0
        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            string errName = string.Format("调用 {0} 时异常", OperationContext.Current.IncomingMessageProperties["HttpOperationName"]);

            WfErrorDTO errorDTO = new WfErrorDTO()
            {
                Number      = 100,
                Name        = errName,
                Message     = error.Message,
                Description = error.StackTrace
            };

            string jsonResult = string.Empty;

            try
            {
                jsonResult = JSONSerializerExecute.SerializeWithType(errorDTO);
            }
            catch (InvalidOperationException)
            {
                errorDTO.Description = string.Empty;
                jsonResult           = JSONSerializerExecute.SerializeWithType(errorDTO);
            }

            fault = WcfUtils.CreateJsonFormatReplyMessage(version, null, jsonResult);
        }
        public System.ServiceModel.Channels.Message SerializeReply(System.ServiceModel.Channels.MessageVersion messageVersion, object[] parameters, object result)
        {
            Message returnMessage = null;

            PerformanceMonitorHelper.GetDefaultMonitor().WriteExecutionDuration("SerializeReply", () =>
            {
                //返回值总是Raw格式的
                string jsonResult = string.Empty;

                if (result is string)
                {
                    if (this._AtlasEnabled)
                    {
                        //asp.net ajax 返回值格式
                        Dictionary <string, object> returnDict = new Dictionary <string, object>();
                        returnDict.Add("d", result);
                        jsonResult = JSONSerializerExecute.Serialize(returnDict);
                    }
                    else
                    {
                        jsonResult = result.ToString();
                    }
                }
                else
                {
                    jsonResult = JSONSerializerExecute.SerializeWithType(result);
                }

                returnMessage = WcfUtils.CreateJsonFormatReplyMessage(messageVersion, this._OperationDesc.Messages[1].Action, jsonResult);
            });

            return(returnMessage);
        }
        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);
            }
        }
Esempio n. 4
0
        /// <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);
            }
        }
Esempio n. 5
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;
        }
Esempio n. 6
0
        /// <summary>
        /// 序列化服务调用的请求信息
        /// </summary>
        /// <param name="messageVersion"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public System.ServiceModel.Channels.Message SerializeRequest(System.ServiceModel.Channels.MessageVersion messageVersion, object[] parameters)
        {
            Dictionary <string, object> paramNameValuePair = new Dictionary <string, object>();

            for (int i = 0; i < _OperationDesc.Messages[0].Body.Parts.Count; i++)
            {
                string paramName = _OperationDesc.Messages[0].Body.Parts[i].Name;
                object paramVal  = parameters[i];

                paramNameValuePair.Add(paramName, paramVal);
            }

            paramNameValuePair["__ConnectionMappings"] = GetConnectionMappings();
            paramNameValuePair["__Context"]            = WfClientServiceBrokerContext.Current.Context;

            //if (WfClientServiceBrokerContext.Current.Context.ContainsKey("TenantCode") == false &&
            //    TenantContext.Current.Enabled)
            if (TenantContext.Current.Enabled)
            {
                WfClientServiceBrokerContext.Current.Context["TenantCode"] = TenantContext.Current.TenantCode;
            }

            IGenericTokenPrincipal principal = GetPrincipal(WfClientServiceBrokerContext.Current);

            if (principal != null)
            {
                paramNameValuePair["__TokenContainer"] = principal.GetGenericTicketTokenContainer();
            }

            string  paramJson      = JSONSerializerExecute.SerializeWithType(paramNameValuePair);
            Message requestMessage = WcfUtils.CreateJsonFormatRequestMessage(messageVersion, _OperationDesc.Messages[0].Action, paramJson);

            requestMessage.Headers.To = _MessageDestinationUri;

            return(requestMessage);
        }