public void DeserializeRequest(Message message, object[] parameters) { object propObj; object[] bodyParameters = new object[parameters.Length - this.totalNumUTVars]; if (bodyParameters.Length != 0) { if (message.Properties.TryGetValue(HttpRequestMessageProperty.Name, out propObj)) { var prop = (HttpRequestMessageProperty) propObj; var contenttype = prop.Headers[HttpRequestHeader.ContentType]; if (contenttype != null) { if (jsonContentTypes.IsMatch(contenttype)) { JsonSerializer js = new JsonSerializer(); js.Deserialize(operation, parameterNames, message, parameters); return; } if (protoContentTypes.IsMatch(contenttype)) { ProtobufSerializer js=new ProtobufSerializer(); js.Deserialize(operation, parameterNames, message, parameters); return; } } } XmlSerializer x = new XmlSerializer(); x.Deserialize(operation, parameterNames, message, parameters); } int j = 0; UriTemplateMatch utmr = null; string UTMRName = "UriTemplateMatchResults"; if (message.Properties.ContainsKey(UTMRName)) { utmr = message.Properties[UTMRName] as UriTemplateMatch; } else { if (message.Headers.To != null && message.Headers.To.IsAbsoluteUri) { utmr = this.uriTemplate.Match(this.baseAddress, message.Headers.To); } } NameValueCollection nvc = (utmr == null) ? new NameValueCollection() : utmr.BoundVariables; for (int i = 0; i < parameters.Length; ++i) { if (this.pathMapping.ContainsKey(i) && utmr != null) { parameters[i] = nvc[this.pathMapping[i]]; } else if (this.queryMapping.ContainsKey(i) && utmr != null) { string queryVal = nvc[this.queryMapping[i].Key]; parameters[i] = this.qsc.ConvertStringToValue(queryVal, this.queryMapping[i].Value); } else { parameters[i] = bodyParameters[j]; ++j; } } }
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) { Message request = OperationContext.Current.RequestContext.RequestMessage; object propObj; if (result is Stream) { byte[] data; if (result is MemoryStream) { data = ((MemoryStream) result).ToArray(); } else { using (MemoryStream ms = new MemoryStream()) { ((Stream)result).CopyTo(ms); data=ms.ToArray(); } } System.ServiceModel.Channels.Message replyMessage = System.ServiceModel.Channels.Message.CreateMessage(messageVersion, operation.Messages[1].Action, new RawBodyWriter(data)); replyMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw)); return replyMessage; } if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out propObj)) { var prop = (HttpRequestMessageProperty) propObj; var accept = prop.Headers[HttpRequestHeader.Accept]; if (accept != null) { if (jsonContentTypes.IsMatch(accept)) { JsonSerializer j = new JsonSerializer(); return j.Serialize(operation, messageVersion, parameters, result); } if (protoContentTypes.IsMatch(accept)) { ProtobufSerializer j=new ProtobufSerializer(); return j.Serialize(operation, messageVersion, parameters, result); } } } XmlSerializer x=new XmlSerializer(); return x.Serialize(operation, messageVersion, parameters, result); }