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?