locked
REST and JSON in a WCF Service. RRS feed

  • Question

  • I'm quite new to WCF and I'm looking to do something specific that I need a bit of help with.

    I want to write a WCF service that communicates to a client through REST rather than SOAP. I also want to have this service send its data back as JSON rather than XML. Can anyone give me some tips on how to do this with WCF. I'm learning fairly quickly but I feel like I need a point in the right direction to get this to work.

    Any help appreciated.
    Thanks,
    Adam
    Sunday, November 4, 2012 7:08 PM

Answers

  • Look up how to use Webhttpbinding.  Here is a good intro: http://msdn.microsoft.com/en-us/magazine/dd315413.aspx.   If you want to send your response back as either JSON or XML you can either configure your service operation with the return message format or specify an endpoint behavior with your outgoing message format.

    for example via config:

    <behaviors>      
        <endpointBehaviors>
           <behavior name="JsonWebHttpBehavior">
              <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
            </behavior>
            <behavior name="PoxWebHttpBinding">
              <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Xml" />
           </behavior>
         </endpointBehaviors>
     </behaviors>

    Please remember to mark as answer.
    • Proposed as answer by Aioriya Monday, November 5, 2012 11:21 PM
    • Edited by Aioriya Monday, November 5, 2012 11:42 PM
    • Marked as answer by LeoTang Wednesday, November 14, 2012 3:26 AM
    Monday, November 5, 2012 11:17 PM