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);
}
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With