public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) { Message reply = Message.CreateMessage(messageVersion, (string)null); SuppressReplyEntityBody(reply); if (useJson && WebHttpBehavior.TrySetupJavascriptCallback(callbackParameterName) != null) { reply.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); } return(reply); }
void SerializeBody(XmlDictionaryWriter writer, object returnValue, object[] parameters, bool isRequest) { if (writer == null) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer")); } if (parameters == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("parameters")); } MessageInfo messageInfo; if (isRequest) { messageInfo = requestMessageInfo; } else { messageInfo = replyMessageInfo; } if (messageInfo.WrapperName != null) { if (WebHttpBehavior.TrySetupJavascriptCallback(callbackParameterName) != null) { writer.WriteStartElement(JsonGlobals.rootString); } else { writer.WriteStartElement(messageInfo.WrapperName, messageInfo.WrapperNamespace); } writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.objectString); } if (messageInfo.ReturnPart != null) { SerializeParameter(writer, messageInfo.ReturnPart, returnValue); } SerializeParameters(writer, messageInfo.BodyParts, parameters); if (messageInfo.WrapperName != null) { writer.WriteEndElement(); } }
public void BeforeSendReply(ref Message reply, object correlationState) { WebBodyFormatMessageProperty formatProperty; JavascriptCallbackResponseMessageProperty javascriptCallbackResponseMessageProperty = null; if (reply.Properties.TryGetValue <WebBodyFormatMessageProperty>(WebBodyFormatMessageProperty.Name, out formatProperty) && formatProperty != null && formatProperty.Format == WebContentFormat.Json) { if (!reply.Properties.TryGetValue <JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptCallbackResponseMessageProperty) || javascriptCallbackResponseMessageProperty == null) { javascriptCallbackResponseMessageProperty = WebHttpBehavior.TrySetupJavascriptCallback(this.CallbackParameterName); if (javascriptCallbackResponseMessageProperty != null) { reply.Properties.Add(JavascriptCallbackResponseMessageProperty.Name, javascriptCallbackResponseMessageProperty); } } if (javascriptCallbackResponseMessageProperty != null) { HttpResponseMessageProperty property; if (reply.Properties.TryGetValue <HttpResponseMessageProperty>(HttpResponseMessageProperty.Name, out property) && property != null) { property.Headers[HttpResponseHeader.ContentType] = applicationJavaScriptMediaType; if (javascriptCallbackResponseMessageProperty.StatusCode == null) { javascriptCallbackResponseMessageProperty.StatusCode = property.StatusCode; } property.StatusCode = HttpStatusCode.OK; if (property.SuppressEntityBody) { property.SuppressEntityBody = false; Message nullJsonMessage = WebOperationContext.Current.CreateJsonResponse <object>(null); nullJsonMessage.Properties.CopyProperties(reply.Properties); reply = nullJsonMessage; } } } } }
void SerializeBareMessageContract(XmlDictionaryWriter writer, object[] parameters, bool isRequest) { bool useAspNetJsonWrapper = WebHttpBehavior.TrySetupJavascriptCallback(callbackParameterName) == null && useAspNetAjaxJson; if (writer == null) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer")); } if (parameters == null) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("parameters")); } MessageInfo messageInfo; if (isRequest) { messageInfo = this.requestMessageInfo; } else { messageInfo = this.replyMessageInfo; } if (useAspNetJsonWrapper && !isRequest) { writer.WriteStartElement(JsonGlobals.rootString); writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.objectString); if (messageInfo.BodyParts.Length == 0) { WriteVoidReturn(writer); } } if (messageInfo.BodyParts.Length > 0) { PartInfo part = messageInfo.BodyParts[0]; DataContractJsonSerializer serializer = part.Serializer as DataContractJsonSerializer; if (useAspNetJsonWrapper && !isRequest) { serializer = RecreateDataContractJsonSerializer(serializer, JsonGlobals.dString); } else { serializer = RecreateDataContractJsonSerializer(serializer, JsonGlobals.rootString); } object graph = parameters[part.Description.Index]; try { serializer.WriteObject(writer, graph); } catch (SerializationException sx) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( System.ServiceModel.SR.GetString(System.ServiceModel.SR.SFxInvalidMessageBodyErrorSerializingParameter, part.Description.Namespace, part.Description.Name, sx.Message), sx)); } } if (useAspNetJsonWrapper && !isRequest) { writer.WriteEndElement(); } }
protected override void SerializeBody(XmlDictionaryWriter writer, MessageVersion version, string action, MessageDescription messageDescription, object returnValue, object[] parameters, bool isRequest) { if ((isRequest && this.isBareMessageContractRequest) || (!isRequest && isBareMessageContractReply)) { SerializeBareMessageContract(writer, parameters, isRequest); } else { bool isJsonp = WebHttpBehavior.TrySetupJavascriptCallback(callbackParameterName) != null; bool useAspNetJsonWrapper = !isJsonp && useAspNetAjaxJson; if (isRequest || (isWrapped && !useAspNetJsonWrapper)) { SerializeBody(writer, returnValue, parameters, isRequest); } else { if (useAspNetJsonWrapper) { writer.WriteStartElement(JsonGlobals.rootString); writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.objectString); } if (useAspNetJsonWrapper && messageDescription.IsVoid) { WriteVoidReturn(writer); } else if (isJsonp && messageDescription.IsVoid) { writer.WriteStartElement(JsonGlobals.rootString); writer.WriteAttributeString(JsonGlobals.typeString, JsonGlobals.nullString); writer.WriteEndElement(); } else if (replyMessageInfo.ReturnPart != null) { DataContractJsonSerializer serializer = replyMessageInfo.ReturnPart.Serializer as DataContractJsonSerializer; if (useAspNetJsonWrapper) { serializer = RecreateDataContractJsonSerializer(serializer, JsonGlobals.dString); } else { serializer = RecreateDataContractJsonSerializer(serializer, JsonGlobals.rootString); } try { serializer.WriteObject(writer, returnValue); } catch (SerializationException sx) { throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( System.ServiceModel.SR.GetString(System.ServiceModel.SR.SFxInvalidMessageBodyErrorSerializingParameter, replyMessageInfo.ReturnPart.Description.Namespace, replyMessageInfo.ReturnPart.Description.Name, sx.Message), sx)); } } else if (replyMessageInfo.BodyParts != null) { SerializeBody(writer, returnValue, parameters, isRequest); } if (useAspNetJsonWrapper) { writer.WriteEndElement(); } } } }