2

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...

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
zam6ak
  • 7,229
  • 11
  • 46
  • 84

2 Answers2

3

Create an input parameter of type XElement and you can query the XML any way you want.

Maurice
  • 27,582
  • 5
  • 49
  • 62
3

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.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171