RESTful Web 服务主体格式 [英] RESTful web service body format

查看:25
本文介绍了RESTful Web 服务主体格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WCF 的新手.我正在做一些简单的RESTful WCF 操作合同.而且,我有一个关于属性类 WebInvoke 的属性 BodyStyle 的选项的问题.一种是WebMessageBodyStyle.Bare,另一种是WebMessageBodyStyle.Wrapped.

I am new to WCF. I am doing some simple RESTful WCF operation contracts. And, I have a question about options for property BodyStyle of attribute class WebInvoke. One option is WebMessageBodyStyle.Bare, and the other is WebMessageBodyStyle.Wrapped.

  • 我什么时候应该使用Bare?
  • 什么时候应该使用Wrapped?

感谢您的帮助.

推荐答案

假设您有一些 XML 请求/响应契约和一些简单的数据契约:

Assume that you have some contract with XML request/response and some simple data contract:

[ServiceContract]
public interface IService
{
    ...
    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Entity DoWork(Entity entity);
    ...
}

[DataContract]
public class Entity
{
    [DataMember]
    public string Name;

    [DataMember]
    public string Value;
}

根据 BodyStyleRequestFormatResponseFormat 的组合,您将拥有不同的格式,但通常:

Depending on the combination of BodyStyle, RequestFormat and ResponseFormat you will have different formats but in general:

JSONWebMessageBodyStyle.Bare 请求和响应将是:

JSON and WebMessageBodyStyle.Bare request and response will be:

请求:

{"Name":"name","Value":"value"}

回复:

{"Name":"ResultName:name","Value":"ResultValue:value"}

JSONWebMessageBodyStyle.Wrapped 请求和响应将是:

JSON and WebMessageBodyStyle.Wrapped request and response will be:

请求:

{"entity":{"Name":"name","Value":"value"}}

回复:

{"DoWorkResult":{"Name":"name","Value":"value"}}

注意:您可以将默认的 DoWorkResult 名称更改为您自己的:

Note: you can change default DoWorkResult name to you own:

[return: MessageParameter(Name = "MyResult")]
Entity DoWork(Entity entity);`

所以从现在开始:

{"MyResult":{"Name":"name","Value":"value"}}

XMLWebMessageBodyStyle.Bare 请求和响应将是:

XML and WebMessageBodyStyle.Bare request and response will be:

请求:

<Entity xmlns="http://schemas.datacontract.org/2004/07/WcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <Name>name</Name>
   <Value>value</Value>
</Entity>

回复:

<Entity xmlns="http://schemas.datacontract.org/2004/07/WcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <Name>name</Name>
   <Value>value</Value>
</Entity> 

XMLWebMessageBodyStyle.Wrapped 请求和响应将是:

XML and WebMessageBodyStyle.Wrapped request and response will be:

请求:

 <DoWork xmlns="http://tempuri.org/">
   <entity>
      <Name>name</Name>
      <Value>value</Value>
   </entity>
 </DoWork>

回复:

 <DoWorkResponse xmlns="http://tempuri.org/">
   <DoWorkResult xmlns:a="http://schemas.datacontract.org/2004/07/WcfService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <a:Name>name</a:Name>
      <a:Value>value</a:Value>
   </DoWorkResult>
 </DoWorkResponse> 

注意:您还可以使用 return: MessageParameter

要回答您的问题,您应该使用哪种 WebMessageBodyStyle 取决于您的需要,这里没有黄金法则.为了互操作性,有时可能需要一种或另一种格式.但是请记住裸体样式的一个限制:由于只有一个 XML 格式的根和一个 JSON 格式的对象,因此只能将一个参数传递给方法.事实上,如果您将服务合同更改为以下内容:

To answer to your question, which WebMessageBodyStyle you should use depends on your needs and there is no golden rule here. For interoperability, one or another format can be sometimes required. But keep in mind about one limitation of bare body style: as there is only one root in XML format and one object in JSON format, only one parameter can be passed to a method. In fact, if you changed a service contract to something like:

[OperationContract]
[WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare)]
Entity DoWork(string id, Entity entity);

服务会抛出异常:

操作 '' of contract '' 指定多个请求体参数在没有任何包装元素的情况下进行序列化.最多一个身体参数可以在没有包装元素的情况下被序列化.要么删除额外的 body 参数或设置 BodyStyle 属性要包装的 WebGetAttribute/WebInvokeAttribute.

Operation '' of contract '' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.

这篇关于RESTful Web 服务主体格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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