开发者

WebGet and non WebGet methods in WCF Rest Service

开发者 https://www.devze.com 2023-03-04 13:35 出处:网络
Following is my Contract and the Oper开发者_开发技巧ationContracts, my issue is when I\'m going with WebGet attribute to all the methods my service is working fine, when I remove WebGet Attribute to a

Following is my Contract and the Oper开发者_开发技巧ationContracts, my issue is when I'm going with WebGet attribute to all the methods my service is working fine, when I remove WebGet Attribute to any one of the OperationContracts im getting following error.

Operation 'ProductDetails' of contract 'IDemo' 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.

These are my methods

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]

Is it mandatory to include [WebGet] attribute to all the operation contracts if we go for 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);
}


The error message really says exactly what the problem is:

Operation 'ProductDetails' of contract 'IDemo' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements.

You cannot have methods which expect more than one parameter, unless you wrap those, e.g. by specifying the BodyStyle setting in the WebGet attribute.

So yes: either you have to apply a [WebGet] to each method of your REST service, or you can reorganize your methods to take in only a single parameter (e.g. by wrapping up the two or three parameters you have now into a single class that holds those multiple parameters, and then passing in an object instance of that Request class).

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

[OperationContract]
string AddNumbers(AddNumbersRequest request);
0

精彩评论

暂无评论...
验证码 换一张
取 消