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.
You can use a REST endpoint to return JSON.
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.
Now with WCF REST support, bare AJAX messaging becomes a first class citizen in WCF.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With