问题
I am building a webservice using RESTful approach and am using WCF WebHttp API (.NET v4). To satisfy some legacy functionality I need to accept raw XML message via POST and process it..For example one of my methods looks like:
[WebInvoke(UriTemplate = "Hello", Method = "POST")]
public Message ProcessMessage(string xmlMessage)
{
if (String.IsNullOrWhiteSpace(xmlMessage))
{
return WebOperationContext.Current.CreateXmlResponse(ProcessingFailedReply);
}
var message = XElement.Parse(xmlMessage);
return WebOperationContext.Current.CreateXmlResponse(ProcessingSuccessfullReply);
}
However, every time I try to POST some xml to "/Hello" I get a message that the format is invalid and it wants specifically encoded string. I guess the API is using standard schema to automatically serialize xmlMessage. When I visit the help ("/help") I am given an example format for my xmlMessage:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string>
How do I allow and process the POSTed request as raw in this scenario? I looked over the API and the only relevant class (WebOperationContext.Current.IncommingRequest) does not have any methods to retrieve raw message...
Thanks Z...
回答1:
Create an input parameter of type XElement and you can query the XML any way you want.
回答2:
In addition to the suggestion from Maurice (which works for XML content), if you want the raw bytes in any content type, you can use a Stream parameter (more information at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx) and it will map the whole request body to that parameter.
来源:https://stackoverflow.com/questions/6584872/get-raw-xml-from-post-method-in-service-implemented-using-wcf-webhttp-api