views:

2163

answers:

5

Consider following example :

public class SomeBusinessLayerService : DataService<MyEntityContainer>
{
 [WebInvoke]
 void DoSomething(string someParam)
 {
 }
}

I can not find example or any help on how can I pass parameter to the function! Using WebClient (or goofinfg around with fiddler) I can trigger the function call, but no matter what I try the parameter someParam is always null What's worse - if I change the type to int - all my attempts end in following error:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"&gt;
  <code></code>
  <message xml:lang="en-US">Bad Request - Error in query syntax.</message>
</error>

Can anyone please help with working example of POST content on how I can call it ?

NOTE: This is DataService, and not WCF service. I can get it working with WCF service same method without any problems.

EDIT: Also I need example of POST and not embedding parameter in URI because URI has size limit and requirement to sanitize the string.

A: 

I have never received a response from Microsoft team to that question, so I found following workaround:

Create a table called FunctionRequest with RequestID, Result and Paramerter string
To perform request I simply create object.
If I need to send complex object - I use DataContext serialization to send it to the server.
All the calls are now also recorded in the database - which is great for logging.

A: 

May be you should try to embrace param in URI with quotes (DoSomething?someParam='param'). I tried this and it works fine, in other case I was receiving the same error as you.

Note: I need POST example, and not URI - URI has many other limitations such as size and requirement to sanitize string.
+2  A: 

After hours of also pulling my hair out with "Bad Request - Error in query syntax" responses, I finally got it working by passing the parameters in the URI.

Pay particular attention to the quotes around string parameters:

WRONG: DoSomething?someParam=param CORRECT: DoSomething?someParam='param'

antscode
A: 

To complete a little antscode response not only you must pay close attention about surrounding the parameter values with quotes ...

BUT! You must also prefix Guid values with "guid". Like so :

var y = ctx.Execute<bool>(new Uri("ReportExists?id=guid'" + Guid.NewGuid() + "'", UriKind.Relative));
Andrei Rinea
That's still URL request, and I specifically need POST example.
Yes, michaelf1977, that's true :( But having had problems with the GUID prefix I thought it was worth mentioning here.
Andrei Rinea
A: 

I had a similar problem, I was sending form parameters in x-www-form-urlencoded format (param1=value1&param2=value2...) using POST to a [WebInvoke] marked method, and had a hard time become I figured out how to get the parameters. I finally achieved it in the following way:

[WebInvoke]
void DoSomething(Stream requestStream)
{
    StreamReader reader = new StreamReader(stream);
    var requestContent = reader.ReadToEnd();
    var parameters = HttpUtility.ParseQueryString(requestContent);
    var param1 = parameters["param1"];
    var param2 = parameters["param2"]; //and so on...
}
Konamiman