Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebGet and non WebGet methods in WCF Rest Service

Tags:

rest

wcf

Following is my Contract and the OperationContracts, 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);
}
like image 835
venkat Avatar asked May 06 '11 05:05

venkat


People also ask

What is the difference between WebGet and WebInvoke?

The main difference between WebGet and WebInvoke is that WebGet is used to retrieve data while WebInvoke is used to update data. WCF stands for Windows Communication Foundation developed by Microsoft. It is used to develop service-oriented applications.

What is WebInvoke method in WCF?

WebInvoke Method Type = POST as we are implementing POST. URI Template defines the URL format by which this method is identified/linked. Note: MethodName, URI Template name & Operation Contract names may not be same means, they can be different. PostSampleMethod will accept XML string as input in POST method.

What are RESTful WCF services?

RESTful service follows the REST (Representational State Transfer) architectural style. WCF service will allows to make calls and exchange the data using SOAP protocol over different protocols (HTTP, TCP, MSMQ etc..) and it uses the complex mechanism like SOAP for communication.


1 Answers

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);
like image 193
marc_s Avatar answered Sep 23 '22 11:09

marc_s