views:

394

answers:

1

I am trying to pass a JSON array to a WCF service. But it doesn't seem to work. I actually pulled an array [GetStudents] out the service and sent the exact same array back to the service [SaveStudents] and nothing (empty array) was received. The JSON array is of the format:

[
  {"Name":"John","Age":12},
  {"Name":"Jane","Age":11},
  {"Name":"Bill","Age":12}
]

And the contracts are of the following format:

//Contracts
[DataContract]
public class Student{
  [DataMember]public string Name { get; set; }
  [DataMember]public int Age{ get; set; }
}

[CollectionDataContract(Namespace = "")]
public class Students : List<Student>
{
  [DataMember]public Endorsements() { }
  [DataMember]public Endorsements(IEnumerable<Student> source) : base(source) { }
}

//Operations
public Students GetStudents()
{
  var result = new Students();
  result.Add(new Student(){Name="John",12});
  result.Add(new Student(){Name="Jane",11});
  result.Add(new Student(){Name="Bill",12});
  return result;
}

//Operations
public void SaveStudents(Students list)
{
  Console.WriteLine(list.Count); //It always returns zero
}

It there a particular way to send an array to a WCF REST service?

+1  A: 

I had similar issue. I was calling the service from a browser and the problem was Firefox dynamically changing the request content-type from 'application/json' to 'application-json;charset=utf-8'. If you are calling the service from a browser, test it with non-firefox browser and if that was the case you need to remove the charset from the request content-type header

Ash
@Ash: That was my exact problem. The service works from Google Chrome but fails in FireFox. Is there any workaround for this: I have no control over the REST service!
Tawani
I couldn't find a client side solution.is xml type (application/xml) an option?
Ash