Get raw xml from POST method in service implemented using WCF WebHttp API

十年热恋 提交于 2019-12-11 03:45:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!