WCF service method to return json or soap
I have been reading lot of posts on retrieving/returning json objects in a WCF method. Correct me if I am wrong: Adding an endpoint and WebHTTp behavior in the config in addition to WebInvoke before a service method enables json objects to be returned by a service method.
The use of webinvoke makes the method very specific to a certain format (json here). The issue is I already have a WCF 开发者_高级运维SOAP service and I want to reuse the service methods to be able to return both XML or JSON objects. Is there a way to make the methods generic and change the response format based on the endpoints used or the platforms used to access my service method?
Yes it is possible. JSON and SOAP need different bindings so your service needs two endpoints - one with webHttpBinding
and webHttp
endpoint behavior and second with basicHttpBinding
or other SOAP oriented binding. These endpoints must have different relative addresses.
If you want to support both JSON and XML (POX not SOAP) formats in REST service you can do it on the same endpoint in WCF 4 by defining automaticFormatSelectionEnabled="true"
in the webHttp
behavior used for the REST endpoint. This allows the endpoint to return the data formatted either as JSON or as XML. The choice of the format is based on the format of incoming request so a request in JSON will get a response in JSON and a request in XML will get a response in XML.
精彩评论