tags:

views:

43

answers:

0

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"&gt;&lt;data&gt;0&lt;/data&gt;&lt;error&gt;false&lt;/error&gt;&lt;msgerror&gt;&lt;/msgerror&gt;&lt;/responsedata&gt;

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