Wednesday, July 9, 2008

WCF bare unwrapped Response

Many times we have seen that a WCF method of return type string spits Response which is wrapped in a String element e.g. the one shown below

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<Company>Nucleus</Company></string>

We may want it to return pure Xml Response, the unwrapped one like

<Company>Nucleus</Company>

These are steps you should use to achieve it

1. Change the webmethod return type from string to System.IO.Stream
2. Remove any BodyStype property from its attributes because even the barest BodyStyle will return wrapped response.
3. Return a Stream instead of string from your webmethod. This can be done with the following simple code
return new MemoryStream(Encoding.UTF8.GetBytes(strRetXml));

No comments: