织梦 xml网站地图,全国信用企业公示系统,和wordpress价值,企业系统规划可以说WebHttpBinding和WebHttpBehavior是整个Web HTTP编程模型最为核心的两个类型#xff0c;前者主要解决消息编码问题#xff0c;而余下的工作基本上落在了终结点行为WebHttpBehavior上。WebHttpBehavior属性HelpEnabled和AutomaticFormatSelectionEnabled是“帮助页面”与…可以说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: configuration2: system.serviceModel3: behaviors4: endpointBehaviors5: behavior6: webHttp helpEnabledtrue /7: /behavior8: /endpointBehaviors9: /behaviors10: services11: service nameArtech.WcfServices.Service.EmployeesService12: endpoint addresshttp://127.0.0.1:3721/employees13: bindingwebHttpBinding 14: contractArtech.WcfServices.Service.Interface.IEmployees/15: /service16: /services17: /system.serviceModel18: /configuration
二、 自动消息格式选择
REST服务具有两种基本的消息格式Xml和Json。在定义服务契约的时候我们可以通过应用在操作方法上的WebGetAttribute和WebInvokeAttribute指定回复消息的格式。如果没有通过这种方式对消息格式进行显式设置我们还可以通过终结点行为WebHttpBehavior为回复消息设置一个默认的消息格式。除了这种显示设置方式之外WCF还提供一种自动消息格式选择机制。
所谓消息格式的自动选择就是服务根据请求消息来选择一种适合的格式进行消息的序列化。在默认的情况下这种自动选择机制是关闭的我们需要通过WebHttpBehavior的AutomaticFormatSelectionEnabled属性开启该机制。具体的消息格式选择机制策略顺序如下
如果作为请求的HTTP消息具有Accept报头则根据该报头决定回复消息的格式如果作为请求的HTTP消息具有Content-Type报头则根据该报头决定回复消息的格式如果在定义服务契约时通过WebGetAttribute或者WebInvokeAttribute对回复消息的格式进行了显式设置则采用该消息格式如果通过终结点行为WebHttpBehavior设置了对回复消息的格式进行了显式设置则采用该消息格式采用默认消息格式XmlWebMessageFormat枚举的默认值。
我们同样通过之前创建的EmployeesService的实例来演示消息格式的自动选择机制。如下面的配置片断所示我们将WebHttpBehavior行为应用到了寄宿服务的唯一终结点上并且将AutomaticFormatSelectionEnabled属性设置为True。 1: configuration2: system.serviceModel3: behaviors4: endpointBehaviors5: behavior namewebHttp6: webHttp automaticFormatSelectionEnabledtrue /7: /behavior8: /endpointBehaviors9: /behaviors10: services11: service nameArtech.WcfServices.Service.EmployeesService12: endpoint addresshttp://127.0.0.1:3721/employees 13: behaviorConfigurationwebHttp14: bindingwebHttpBinding 15: contractArtech.WcfServices.Service.Interface.IEmployees/16: /service17: /services18: /system.serviceModel19: /configuration