WCF service method to return json or soap

I read a lot of posts about searching / returning json objects in WCF method. Correct me if I am wrong: adding endpoint and WebHTTp behavior to the configuration in addition to WebInvoke before the service method allows returning json objects by the service method.

Using webinvoke makes the method very specific for a particular format (here json). The problem is that I already have a WCF SOAP service and I want to reuse the service methods in order to be able to return XML or JSON objects. Is there a way to make general methods and change the response format based on the endpoints or platforms used to access my service?

+3
source share
1 answer

Yes it is possible. JSON and SOAP need different bindings, so your service needs two endpoints: one with the webHttpBinding and webHttp , and the second with basicHttpBinding or another SOAP binding. These endpoints must have different relative addresses.

If you want to support JSON and XML (POX not SOAP) formats in the REST service, you can do this on the same endpoint in WCF 4 by webHttp automaticFormatSelectionEnabled="true" in the webHttp behavior used for the REST endpoint. This allows the endpoint to return data formatted as either JSON or XML. The choice of format is based on the format of the incoming request, so the request in JSON will receive a response in JSON, and the request in XML will receive a response in XML.

+6
source

Source: https://habr.com/ru/post/1444843/


All Articles