Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The usage of Extending Encoders and Serializers (see <a href="http://msdn.microsoft.com/en-us/library/ms733092.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms733092.aspx</a>) or other methods of Extending WCF like usage of <code>DataContractSerializerOperationBehavior</code> is very interesting, but for your special problem there are easier solution ways.</p> <p>If you already use <code>Message</code> type to return the results an use WCF4 you can do something like following:</p> <pre><code>public Message UpdateCity(string code, City city) { MyResponseDataClass message = CreateMyResponse(); // use JSON.NET to serialize the response data string myResponseBody = JsonConvert.Serialize(message); return WebOperationContext.Current.CreateTextResponse (myResponseBody, "application/json; charset=utf-8", Encoding.UTF8); } </code></pre> <p>In case of errors (like <code>HttpStatusCode.Unauthorized</code> or <code>HttpStatusCode.Conflict</code>) or in other situations when you need to set a HTTP status code (like <code>HttpStatusCode.Created</code>) you can continue to use <code>WebOperationContext.Current.OutgoingResponse.StatusCode</code>.</p> <p>As an alternative you can also return a <code>Stream</code> (see <a href="http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx" rel="noreferrer">http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx</a> and <a href="http://msdn.microsoft.com/en-us/library/ms732038.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms732038.aspx</a>) instead of <code>Message</code> to return any data without additional default processing by Microsoft JSON serializer. In case of WCF4 you can use <code>CreateStreamResponse</code> (see <a href="http://msdn.microsoft.com/en-us/library/dd782273.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/dd782273.aspx</a>) instead of <code>CreateTextResponse</code>. Don't forget to set stream position to 0 after writing in the stream if you will use this technique to produce the response.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload