Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF REST return single method as JSON and XML

Tags:

.net

wcf

I am using below Code for WCF Rest Services to get in JSON format

[OperationContract]   

[WebGet(UriTemplate = "/GetOrderList?request={request}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Order> GetOrderList(Request request);

I want this method to return XML type also. Do i need to have one more method for it ? I want to do it in the same method without duplicating the code for XML. I am using WCF 3.5. I cannot change my version.

like image 471
Bokambo Avatar asked Feb 25 '13 04:02

Bokambo


People also ask

Can we return JSON from WCF service?

You can use a REST endpoint to return JSON.

Does WCF use SOAP or REST?

Normally, a WCF service will use SOAP, but if you build a REST service, clients will be accessing your service with a different architectural style (calls, serialization like JSON, etc.). Exposing a WCF service with both SOAP and REST endpoints, requires just a few updates to the codebase and configuration.

Does WCF support Ajax REST?

Now with WCF REST support, bare AJAX messaging becomes a first class citizen in WCF.

Can WCF be RESTful?

You can use WCF to build RESTful services in . NET. REST (Representational State Transfer) is an architecture paradigm that conforms to the REST architecture principles. The REST architecture is based on the concept of resources: It uses resources to represent the state and functionality of an application.


2 Answers

I was having the same issue. We provided the solution by creating two endpoints one for XML and the other for JSON.

Make sure you remove all attributes from the Service Interface. Don't specify the RequestFormat or ResponseFormat to control XML or JSON. Let it be controlled by the endpoint.

Service Web.Config changes.

<endpoint address="XML" binding="webHttpBinding" bindingConfiguration="webHttpBindingXML" contract="xxxxxx.Ixxxxxxx" behaviorConfiguration="RestXMLEndpointBehavior"/>
<endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="webHttpBindingJSON" contract="xxxxxxxx.Ixxxxxxxxx" behaviorConfiguration="RestJSONEndpointBehavior"/>
  <endpointBehaviors>

    <behavior name="RestJSONEndpointBehavior">
      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
    </behavior>
    <behavior name="RestXMLEndpointBehavior">
      <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Xml"/>
    </behavior>

  </endpointBehaviors>        
<webHttpBinding>
<binding name="webHttpBindingXML"/>
<binding name="webHttpBindingJSON"/>
</webHttpBinding>

Hope this helps.

like image 63
Satchi Avatar answered Sep 23 '22 23:09

Satchi


You don't even need to specify the return type here , we have a property called automaticFormatSelectionEnabled for WebGet in end point behaviors as shown below. When you making your rest call request from client, you can specify the type as WebClient.Headers["Content-type"] = "application/json"; or WebClient.Headers["Content-type"] = "application/xml"; , service will detect the type and return the correct format you want ..

  <endpointBehaviors>
        <behavior name="RestServiceEndPointBehavior">
          <webHttp automaticFormatSelectionEnabled="true"   />
        </behavior>
  </endpointBehaviors>
like image 34
Vibin Kesavan Avatar answered Sep 23 '22 23:09

Vibin Kesavan