Pass a JSON array to a WCF web service

Posted by Tawani on Stack Overflow See other posts from Stack Overflow or by Tawani
Published on 2010-03-31T13:48:54Z Indexed on 2010/04/03 21:13 UTC
Read the original article Hit count: 3775

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about wcf

Related posts about wcf-rest