1

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?

3
  • Seeing the JSON you're sending is definitely helpful, but the question is still missing one key piece - the client-side javascript code that you're using to call that you're using to make the call. Do you know for sure that your service method is actually being called? How are you obtaining the JSON you're sending? Commented Sep 21, 2009 at 17:56
  • Yes, I'm using Fiddler to test the method. Updated the original question with some information. Aahhhh, can't see whats wrong!! Thanks for helping me! :-) Commented Sep 21, 2009 at 18:18
  • Finally got it working, see last answer.. Commented Sep 21, 2009 at 18:53

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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;}
}

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.