views:

100

answers:

1

Hi,

I have a WCF service which I would like to product XML and JSON depending on the URI template. So in my service contract I have methods like the following

[ServiceContract]
public interface MultiFormatContract
{
  [OperationContract]
  [WebGet(UriTemplate="/json/data", ResponseFormat = WebMessageFormat.Json)]
  [JSONPBehavior(callback = "callback")]
  public MySerializableObject GetJSONData()


  [OperationContract]
  [WebGet(UriTemplate = "/xml/data", ResponseFormat=WebMessageFormat.Xml)]
  public MySerializableObject GetXMLData()
}

I have a requirement to support JSONP and have used the Microsoft REST sample to support this (following this http://jasonkelly.net/archive/2009/02/24/using-jquery-amp-jsonp-for-cross-domain-ajax-with-wcf-services.aspx). However this has meant that I had to add a custom binding:

  <customBinding>
    <binding name="jsonpBinding">
      <jsonpMessageEncoding/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>

This means WCF is no longer using the textMessageEncoding and even though JSONPEncoder overrides the text encoder. My endpoints can no longer produce Xml.

I don't want to lock consumers to JSON and I'd like to be able to browse to my Xml endpoint to see the Xml for debugging etc.

Any ideas on how I can have both?

+1  A: 

Instead of segregating your response types by creating a 'virtual' endpoint using UriTemplate, e.g. "/xml/data", simply define multiple endpoints with appropriate bindings. You can use a single method and put all of the attributes on one method.

e.g.

<endpoint address="json"....
<endpoint address="jsonp"....
<endpoint address="xml" ....
Sky Sanders
Excellent idea that. Thankyou.
Rob Stevenson-Leggett
@Rob - My pleasure. Been there, done that.
Sky Sanders