Ask a questionAsk a question
 

QuestionI can't pass parameter to [WebInvoke] method

  • Friday, September 18, 2009 2:23 PMSergey Myslyvyy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I need to call ADO.NET Data Services operation method that is marked with [WebInvoke] attribute.
    But I can't pass parameter to this method, it's always null on the server side.
    Parameter must be passed in the body of "POST" request, not in the url (it can be too large for query stirng, 10-50 kb for example).

    I tried to find some example on this forum and "ADO.NET Data Services " forum, but I found only advices to use query string (www.example.com/DataService.svc/method?param=value) or unanswered questions.

    Could somebody give some example?

    My code (not real, only for demonstration):

    Server side

    [WebInvoke]
    public void ValidateData(String data)
    {
        // unfortunately, data is always null
    }


    Client code:

    String param = "data='some data'";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:4216/DataService.svc/ValidateData");
    req.Method = "POST";
    req.ContentLength = param.Length;
    req.ContentType = "application/xml";
    Stream sw = req.GetRequestStream();
    Byte[] bytes = Encoding.ASCII.GetBytes(param);
    sw.Write(bytes, 0, bytes.Length);
    WebResponse resp = req.GetResponse();


    Thanks,
    Sergey

All Replies

  • Monday, September 28, 2009 6:50 PMPeter Qian - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Sergey,

    We actually encode the parameters in the URI itself, you should try:

    http://localhost:4216/DataService.svc/ValidateData?data='some data'

    if your data is over 2kb in length, you can put this request inside a batch request format, and post to /$batch.

    Regards,
    PQ

    Peter Q. http://blogs.msdn.com/peter_qian