public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) { byte[] body; var serializer = NewtonsoftJsonSettings.GetSerializer(); using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms)) { using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw)) { writer.Formatting = NewtonsoftJsonSettings.GetFormatting(); serializer.Serialize(writer, result); sw.Flush(); body = ms.ToArray(); } } } var respProp = new HttpResponseMessageProperty(); respProp.Headers[HttpResponseHeader.ContentType] = "application/json"; var replyMessage = Message.CreateMessage(messageVersion, m_operation.Messages[1].Action, new RawBodyWriter(body)); replyMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw)); replyMessage.Properties.Add(HttpResponseMessageProperty.Name, respProp); return(replyMessage); }
public object DeserializeReply(Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty) == null || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); var serializer = NewtonsoftJsonSettings.GetSerializer(); bodyReader.ReadStartElement("Binary"); byte[] body = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(body)) { using (var sr = new StreamReader(ms)) { Type returnType = this.m_operation.Messages[1].Body.ReturnValue.Type; return(serializer.Deserialize(sr, returnType)); } } }
public Message SerializeRequest(MessageVersion messageVersion, object[] parameters) { byte[] body; var serializer = NewtonsoftJsonSettings.GetSerializer(); using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms)) { using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw)) { writer.Formatting = NewtonsoftJsonSettings.GetFormatting(); if (parameters.Length == 1) { // Single parameter, assuming bare serializer.Serialize(sw, parameters[0]); } else { writer.WriteStartObject(); foreach (MessagePartDescription t in this.m_operation.Messages[0].Body.Parts) { writer.WritePropertyName(t.Name); serializer.Serialize(writer, parameters[0]); } writer.WriteEndObject(); } writer.Flush(); sw.Flush(); body = ms.ToArray(); } } } Message requestMessage = Message.CreateMessage(messageVersion, m_operation.Messages[0].Action, new RawBodyWriter(body)); requestMessage.Headers.To = m_operationUri; requestMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw)); HttpRequestMessageProperty reqProp = new HttpRequestMessageProperty(); reqProp.Headers[HttpRequestHeader.ContentType] = "application/json"; requestMessage.Properties.Add(HttpRequestMessageProperty.Name, reqProp); return(requestMessage); }
public void DeserializeRequest(Message message, object[] parameters) { object bodyFormatProperty; if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) || (bodyFormatProperty as WebBodyFormatMessageProperty) == null || (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw) { throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?"); } var bodyReader = message.GetReaderAtBodyContents(); bodyReader.ReadStartElement("Binary"); var rawBody = bodyReader.ReadContentAsBase64(); using (var ms = new MemoryStream(rawBody)) { using (var sr = new StreamReader(ms)) { var serializer = NewtonsoftJsonSettings.GetSerializer(); if (parameters.Length == 1) { if (m_operation.Messages[0].Body.Parts[0].Type == typeof(string)) { var str = sr.ReadToEnd(); var queryString = HttpUtility.ParseQueryString(str); if (queryString.AllKeys.Contains(m_operation.Messages[0].Body.Parts[0].Name)) { parameters[0] = Convert.ChangeType(queryString[m_operation.Messages[0].Body.Parts[0].Name], m_operation.Messages[0].Body.Parts[0].Type); } } else { parameters[0] = serializer.Deserialize(sr, m_operation.Messages[0].Body.Parts[0].Type); } } else { // multiple parameter, needs to be wrapped Newtonsoft.Json.JsonReader reader = new Newtonsoft.Json.JsonTextReader(sr); reader.Read(); if (reader.TokenType != Newtonsoft.Json.JsonToken.StartObject) { throw new InvalidOperationException("Input needs to be wrapped in an object"); } reader.Read(); while (reader.TokenType == Newtonsoft.Json.JsonToken.PropertyName) { var parameterName = reader.Value as string; if (parameterName == null) { throw new InvalidOperationException("The object contained a parameter, however the value was null."); } reader.Read(); if (this.m_parameterNames.ContainsKey(parameterName)) { int parameterIndex = this.m_parameterNames[parameterName]; parameters[parameterIndex] = serializer.Deserialize(reader, this.m_operation.Messages[0].Body.Parts[parameterIndex].Type); } else { reader.Skip(); } reader.Read(); } reader.Close(); } } } }