WCF服务方法返回json或soap [英] WCF service method to return json or soap

查看:227
本文介绍了WCF服务方法返回json或soap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关使用WCF方法检索/返回json对象的文章.如果我错了,请纠正我:在服务方法允许服务方法返回json对象之前,除了在WebInvoke中在配置中添加终结点和WebHTTp行为.

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.

使用webinvoke使该方法非常特定于某种格式(此处为json).问题是我已经拥有WCF SOAP服务,并且我想重用服务方法以能够返回XML或JSON对象.有没有办法使这些方法通用,并根据使用的端点或用于访问我的服务方法的平台更改响应格式?

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?

推荐答案

是可以的. JSON和SOAP需要不同的绑定,因此您的服务需要两个终结点-一个终结点具有webHttpBindingwebHttp行为,第二个终结点具有basicHttpBinding或其他面向SOAP的绑定.这些端点必须具有不同的相对地址.

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.

如果要在REST服务中同时支持JSON和XML(POX而不是SOAP)格式,则可以通过在用于REST端点的webHttp行为中定义automaticFormatSelectionEnabled="true",在WCF 4的同一端点上执行此操作.这允许端点返回格式为JSON或XML的数据.格式的选择基于传入请求的格式,因此JSON中的请求将获得JSON响应,而XML中的请求将获得XML响应.

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.

这篇关于WCF服务方法返回json或soap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆