WCF Rest Service中的WebGet和非WebGet方法

ven*_*kat 8 rest wcf

以下是我的合同和OperationContracts,我的问题是,当我将WebGet属性用于我的服务正常工作的所有方法时,当我将WebGet属性移除到任何一个OperationContracts时,我会得到以下错误.

合同'IDemo'的操作'ProductDetails'指定要序列化的多个请求体参数,而不包含任何包装元素.最多可以在没有包装元素的情况下序列化一个body参数.删除额外的body参数或将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为Wrapped.

这些是我的方法

string AddNumbers(int x,int y);  --- using [WebGet]

string SubtractNumbers(int x, int y); -- using [WebGet]

String ProductDetails(string sName, int cost, int Quntity, string binding); -- not using using [WebGet]

CompositeType GetDataUsingDataContract(CompositeType composite); -- not using [WebGet]
Run Code Online (Sandbox Code Playgroud)

[WebGet]如果我们去WebHttpbinding,是否必须在所有操作合同中包含属性?

public interface IService1
{
    [OperationContract]        
    string GetData(int value,string binding);

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Xml,
           UriTemplate = "/Add?num1={x}&num2={y}")]
    string AddNumbers(int x,int y);

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Xml,
           UriTemplate = "/Subtract?num1={x}&num2={y}")]
    string SubtractNumbers(int x, int y);

    [OperationContract]
    String ProductDetails(string sName, int cost, int Quntity, string binding);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 17

错误消息确切地说明问题是什么:

合同'IDemo'的操作'ProductDetails'指定要序列化的多个请求体参数,而不包含任何包装元素.最多可以在没有包装元素的情况下序列化一个body参数.

您不能拥有期望多个参数的方法,除非您将其包装,例如通过指定属性中的BodyStyle设置WebGet.

所以是的:要么你必须对[WebGet]你的REST服务的每个方法应用一个,要么你可以重新组织你的方法只接受一个参数(例如,通过将你现在拥有的两个或三个参数包装到一个包含那些参数的类中多个参数,然后传入该Request类的对象实例).

[DataContract]
public class AddNumbersRequest
{
   [DataMember]
   public int X { get; set; }
   [DataMember]
   public int Y { get; set; }
}   

[OperationContract]
string AddNumbers(AddNumbersRequest request);
Run Code Online (Sandbox Code Playgroud)