Example #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);
        }
        /// <summary>
        /// 将ClenteState中的信息生成ClientState字符串
        /// </summary>
        /// <returns>ClientState字符串</returns>
        protected override string SaveClientState()
        {
            string result = string.Empty;

            if (this.SelectedData != null)
            {
                result = JSONSerializerExecute.SerializeWithType(this.SelectedData);
            }

            return(result);
        }
Example #4
0
        public void VoucherWithoutItemCollectionConverterJsonTest()
        {
            VoucherEntity source = VoucherEntity.PrepareData();

            JSONSerializerExecute.RegisterConverter(typeof(VoucherConverter));

            string json = JSONSerializerExecute.SerializeWithType(source, true);

            Console.WriteLine(json);

            VoucherEntity deserialized = JSONSerializerExecute.DeserializeString <VoucherEntity>(json);

            //不校验CollectionName
            AssertVoucherEntity(source, deserialized, false);
        }
Example #5
0
        public void DictObjectJsonTest()
        {
            Dictionary <string, object> source = new Dictionary <string, object>();

            JsonTestObj data = JsonTestObj.PrepareData();

            source.Add("Data", data);

            string json = JSONSerializerExecute.SerializeWithType(source);

            Console.WriteLine(json);

            Dictionary <string, object> deserializedData = JSONSerializerExecute.Deserialize <Dictionary <string, object> >(json);

            AssertObjects((JsonTestObj)source["Data"], (JsonTestObj)deserializedData["Data"]);
        }
Example #6
0
        public void VoucherItemCollectionWithConverterJsonTest()
        {
            JSONSerializerExecute.RegisterConverter(typeof(VoucherConverter));
            JSONSerializerExecute.RegisterConverter(typeof(VoucherItemCollectionConverter));

            VoucherEntity source = VoucherEntity.PrepareData();

            string json = JSONSerializerExecute.SerializeWithType(source.Items, true);

            Console.WriteLine(json);

            JavaScriptSerializer serializer = JSONSerializerFactory.GetJavaScriptSerializer(typeof(VoucherItemCollection));

            VoucherItemCollection deserialized = JSONSerializerExecute.DeserializeString <VoucherItemCollection>(json);

            AssertVoucherItemCollection(source.Items, deserialized);
        }
        /// <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);
        }