1

I need to be able to pass XML to a RESTFul WCF service as a string, however I am struggling to do so. Can someone please let me know how I could do this? It must be sent as a string, I cannot wrap it in a data contract etc. Example of the service contract below

[OperationContract]
[WebInvoke(Method = "POST",
            UriTemplate = "lookup",
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml)]
Stream LookupPostcode(string requestXml);

Many thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
andyJ
  • 1,040
  • 5
  • 19
  • 26

4 Answers4

1

This is completely off-topic, but trying to bend WCF to be RESTful will be a constant battle and you will ultimately surrender. It's the message-oriented RPC- and SOAP-based configure-everything-in-XML nature of WCF that makes it so hard to write simple REST services.

If you're not neck-deep in your project, try researching other alternatives and abandon WCF for that purpose.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • That was the case in WCF 3.5 and WCF 4, but the new WCF Web API here http://wcf.codeplex.com is a completely different story. It has all the flexibility you need and is very HTTP compliant. – Darrel Miller Apr 18 '11 at 12:45
  • @Darrel Never used WCF 4 myself, but `public JsonValue GetAll()` and sheer amount of bloat code around this kills me. – Anton Gogolev Apr 18 '11 at 12:51
  • 2
    If simple is what you want, then I have just what you need :-) http://www.bizcoder.com/index.php/2011/04/16/the-worlds-simplest-wcf-web-api/ – Darrel Miller Apr 18 '11 at 12:54
  • @Darrel Now that's something, but I'm still not convinced. WCF's approach to REST has all kinds of inherent architectural flaws that make it unsuitable at least for me. But your code is real nice. – Anton Gogolev Apr 18 '11 at 13:00
  • I certainly understand your skepticism, but the new WebAPI stuff has been severely rearchitected and done under the guidance of some heavy duty REST people from all different backgrounds. It is not a cosmetic enhancement. – Darrel Miller Apr 18 '11 at 13:25
1

Try using XElement or Stream as your method parameter.

[OperationContract] 
[WebInvoke(Method = "POST",
           UriTemplate = "lookup",
           BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Xml)] 
Stream LookupPostcode(Stream requestXml); 

...

[OperationContract] 
[WebInvoke(Method = "POST",
           UriTemplate = "lookup",
           BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Xml)] 
Stream LookupPostcode(XElement requestXml); 

... Not sure what you are trying to do inside of the method or I could probably provide more help.

Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
  • 1
    Were you able to get the Stream parameter to work in WCF 3.5 or was that only in WCF 4? I was never able to get that working in 3.5. – Darrel Miller Apr 18 '11 at 12:57
  • I use it all the time in .Net 3.5. I belive you have to change the binding mode from buffering. – Matthew Whited Apr 18 '11 at 17:01
  • Cool. I think you are referring to TranferMode = Streamed. I always though that only worked for streaming stuff and would not work for regular parameters. – Darrel Miller Apr 18 '11 at 17:14
0

Wrapping the xml in CDATA tags stops the parser from treating it as xml:

myString = "<![CDATA[<thexml/>]]>"
Daz Eddy
  • 332
  • 2
  • 15
0

It's a major hack, but you can wrap your XML inside a <string> tag like this.

 XmlDocument body = new XmlDocument();
 body.Load(...);

 postData = @"<string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'><![CDATA[" + body.OuterXml + "]]></string>";
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243