views:

431

answers:

3

I have seen many examples of json web services with WCF in asp.net that accept one in-parameter. I need my service method to accept many parameters and return a list like this;

This is my Service.svc:

public class Car
{
    public string Title;
    public int Number;
}

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class Service
{

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    public List<Car> GetCarById(int userid, string password , List<Cars> carlist)
    {
        //Doing some stuff here
    }
}

In my web.config:

<system.serviceModel>
   <behaviors>
       <endpointBehaviors>
             <behavior name="ServiceAspNetAjaxBehavior">
                 <enableWebScript />
             </behavior>
       </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
   <services>
         <service name="Service">
             <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior"
 binding="webHttpBinding" contract="Service" />
          </service>
  </services>
  </system.serviceModel>

How can I call this service if I want to pass all these parameters? When I try to call the method with the params userid, password and carlist, they are all null. This is what I send to http://localhost/Service.svc/GetCarById using POST in an application called Fiddler:

HEADERS:
Host: localhost
Content-Length: 136
Content-Type: application/json; charset=utf-8

BODY:
{"d":{"password":"test","userid":123,"carlist":[{"__type":"Car","Number":1,"Title":"BMW"},{"__type":"Car","Number":2,"Title":"Honda"}]}}

The response is: {"d":null} The method is returning carlist, so I know the method is called and that carlist is null. If I change the method to return a string for example "Error" I get that back, so I know the method is called..

Do you have an idea what I'm doing wrong?

A: 

Can you change the signature of your method. If so, why not create a Request object that encapsulates your parameters? Like this:

public class GetCarByIdRequest
{
   public int CarId {get;set;}
   public int Password {get;set;}
   public List<Car> CarList {get;set;}
}
Samuel Jack
Thanks for your help! What I really need to do is to send a List into my method as a parameter. But I can't figure out how to post the data to my method. Do you know where to find an example on how to do that?
Martin
A: 

See original question...

Martin
Martin, it's customary to update your question with new details rather than add an answer to clarify it (or post a new question and edit your original to link to the new one). Having said that, what address is your client trying to post to?
Jeff Sternal
Oh sorry, didn't know, will do that next time. I'm trying to post to: localhost/Service.svc/GetCarById. I found out that I was missing the "d" in the beginning. So now the error is gone, but still I can't get the params in my method, userid, password and List all is null. This is what I send: {"d":[{"password":"test","userid":123,"carlist":[{"__type":"Car","Number":1,"Title":"BMW"},{"__type":"Car","Number":2,"Title":"Honda"}]}]}Do you have an idea what I'm doing wrong?
Martin
Hmmm yes, I thought that too, but that didn't help. But the json seems right? I also tried just to change the method to only accept userid with this json: {"d":{"userid":"123"}}, but still it is null. Strange.. Any ideas?
Martin
Nothing else springs to mind - perhaps edit your original question to include the client code.
Jeff Sternal
+1  A: 

Finally found the solution:

Changed from:

[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

To just:

[WebInvoke]

And then I called the service without the initial "d", everything works perfect!

Thanks for your help!

Martin
Please set this question as solved by yourself
Ramon Araujo