WCF REST消息格式(JSON/XML)选择

可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型,前者主要解决消息编码问题,而余下的工作基本上落在了终结点行为WebHttpBehavior上。WebHttpBehavior属性HelpEnabled和AutomaticFormatSelectionEnabled是"帮助页面"与"自动消息格式选择"这两个特性的总开关。

复制代码
public class WebHttpBehavior : IEndpointBehavior, ...
{
   //其他成员    
    public virtual bool HelpEnabled { get; set; }
     public virtual bool AutomaticFormatSelectionEnabled { get; set; }
}

WCF 4.0为REST服务提供了帮助页面功能,我们可以通过浏览器访问服务帮助页面的地址得到所有操作的基本信息。但是这个功能在默认的情况下是关闭的,我们需要通过应用在终结点上的WebHttpBehavior行为的HelpEnabled属性开启该功能。

复制代码
   1: <configuration>
   2:     <system.serviceModel>
   3:         <behaviors>
   4:             <endpointBehaviors>
   5:                <behavior>
   6:                    <webHttp helpEnabled="true" />
   7:                </behavior>
   8:             </endpointBehaviors>
   9:         </behaviors>
  10:         <services>
  11:             <service name="Artech.WcfServices.Service.EmployeesService">
  12:                 <endpoint address="http://127.0.0.1:3721/employees"
  13:                           binding="webHttpBinding" 
  14:                           contract="Artech.WcfServices.Service.Interface.IEmployees"/>
  15:             </service>
  16:         </services>
  17:     </system.serviceModel>
  18: </configuration>
二、 自动消息格式选择

REST服务具有两种基本的消息格式(Xml和Json)。在定义服务契约的时候,我们可以通过应用在操作方法上的WebGetAttribute和WebInvokeAttribute指定回复消息的格式。如果没有通过这种方式对消息格式进行显式设置,我们还可以通过终结点行为WebHttpBehavior为回复消息设置一个默认的消息格式。除了这种显示设置方式之外,WCF还提供一种自动消息格式选择机制。

所谓消息格式的自动选择,就是服务根据请求消息来选择一种适合的格式进行消息的序列化。在默认的情况下,这种自动选择机制是关闭的,我们需要通过WebHttpBehavior的AutomaticFormatSelectionEnabled属性开启该机制。具体的消息格式选择机制策略(顺序)如下:

  • 如果作为请求的HTTP消息具有Accept报头,则根据该报头决定回复消息的格式;
  • 如果作为请求的HTTP消息具有Content-Type报头,则根据该报头决定回复消息的格式;
  • 如果在定义服务契约时通过WebGetAttribute或者WebInvokeAttribute对回复消息的格式进行了显式设置,则采用该消息格式;
  • 如果通过终结点行为WebHttpBehavior设置了对回复消息的格式进行了显式设置,则采用该消息格式;
  • 采用默认消息格式Xml(WebMessageFormat枚举的默认值)。

我们同样通过之前创建的EmployeesService的实例来演示消息格式的自动选择机制。如下面的配置片断所示,我们将WebHttpBehavior行为应用到了寄宿服务的唯一终结点上,并且将AutomaticFormatSelectionEnabled属性设置为True。

复制代码
  1: <configuration>
   2:     <system.serviceModel>
   3:         <behaviors>
   4:             <endpointBehaviors>
   5:                 <behavior name="webHttp">
   6:                     <webHttp automaticFormatSelectionEnabled="true" />
   7:                 </behavior>
   8:             </endpointBehaviors>
   9:         </behaviors>
  10:     <services>
  11:         <service name="Artech.WcfServices.Service.EmployeesService">
  12:             <endpoint address="http://127.0.0.1:3721/employees" 
  13:                       behaviorConfiguration="webHttp"
  14:                       binding="webHttpBinding"  
  15:                       contract="Artech.WcfServices.Service.Interface.IEmployees"/>
  16:             </service>
  17:         </services>
  18:     </system.serviceModel>
  19: </configuration>
相关推荐
Venuslite4 天前
从 Unexpected token < 到 Extra data:一次讲清 JSON 解析错误的排查思路
json
疯狂SQL10 天前
手写高性能在线 JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
typescript·json·next.js·web worker·前端性能优化·esbuild·源码实战
tianyuanwo15 天前
深入解析 RISC-V 虚拟化中的 UEFI 固件配置:从 XML 到 NVRAM 的生命周期管理
xml·linux·risc-v
terry60015 天前
5G视频短信服务商选型全攻略:通道资源、架构能力与成本评估2026最新标准
大数据·人工智能·5g·json·asp.net·信息与通信·数据库架构
前网易架构师-高司机15 天前
带标注的辣椒病叶数据集,识别率95.9%,可识别三种病害和健康叶子,9916张图,支持yolo,coco json,voc xml,文末有模型训练代码
yolo·json·数据集·病害·叶病·病叶·辣椒
PixelBai16 天前
JSON扁平化使用教程:从入门到精通
json
渔舟唱晚,雁阵惊寒16 天前
CSDN博客内容丢失如何恢复?
json
衣乌安、16 天前
JSON-RPC协议
网络协议·rpc·json
PixelBai16 天前
JSON过滤使用教程:从入门到精通
javascript·chrome·json