Reputation: 3
Hi all and thanks for your time in advance.
I have a problem trying to get to work a REST method under WCF. The method is POST, and I'm not able to retrieve the values sent from the Request.
This is how I declare the service according to an exemple from Microsoft about the WebInvokeAttribute Class:
[OperationContract]
[WebInvoke(
Method = "POST" ,
BodyStyle = WebMessageBodyStyle.Bare ,
UriTemplate = "/sum?x={x}&y={y}" ,
ResponseFormat = WebMessageFormat.Xml )]
ResponseData Sum( string x, string y );
This is how I implemented this function in the class:
public ResponseData Sum( string x , string y )
{
ResponseData retorn = new ResponseData();
int _x = 0;
int _y = 0;
try
{
_x = Convert.ToInt32( x );
_y = Convert.ToInt32( y );
retorn.Data = _x + _y + "";
}
catch ( Exception ex )
{
retorn.Data = "";
retorn.Error = true;
retorn.MsgError = ex.Message;
}
return retorn;
}
ResponseData is a class implementing DataContract:
[DataContract]
public class ResponseData
{
private bool error = false;
private string msgError = "";
[DataMember]
public string Data { get; set; }
[DataMember]
public bool Error
{
get
{
return error;
}
set
{
error = value;
}
}
[DataMember]
public string MsgError
{
get
{
return msgError;
}
set
{
msgError = value;
}
}
}
As you can see, quite a simple example.
The thing is that it doesn't work. Tha x and y parameters of the functions always have a value of null, doesn't matter what I send along the request, so the returning message is always:
<responsedata xmlns="http://schemas.datacontract.org/2004/07/RestServiceProvaCrypto" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><data>0</data><error>false</error><msgerror></msgerror></responsedata>
So my question is, what I'm I doing wrong as I can't obtain the values passed by the Request for x and y?
I'm working with VS 2008 and .Net 3.5.
Again, thanks for your time.
Ramon M. Gallart
Upvotes: 0
Views: 4921
Reputation: 46
I dont know how much this will help you at this time, but according to me you are invoking the wrong CRUD concept for your problem.You should try to use the WebGet attribute and keep your UriTemplate the same, as in fact you are passing the data through a querystring. I would suggest..
[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare ,
UriTemplate = "/sum?x={x}&y={y}" ,
ResponseFormat = WebMessageFormat.Xml )]
ResponseData Sum( string x, string y );
Hope this helps..
Regards, vvn
Upvotes: 3