I was calling my WCF services from JQuery. Basically I was calling $getJSON to retrieve JSON data to display it in a dynamic table.
I started facing problem with the data sequence. It looked like i was not getting data at the same sequence it is in the class. My class looked like that
[DataContract]
public class MyClass
{
public DateTime Interval { get; set; }
public Double Actual { get; set; }
public String Units { get; set; }
public MyClass(DateTime interval, double actual, string units)
{
this.Interval = interval;
this.Actual = actual;
this.Units = units;
}
}
It was returning data in the order of Actual, Interval, Units.
Looks like it sends data sorting by DataMember Name
To Solve the issue we need to add Order option for every DataMember.
[DataContract]
public class MyClass
{
[DataMember(Order = 0)]
public DateTime Interval { get; set; }
[DataMember(Order = 1)]
public Double Actual { get; set; }
[DataMember(Order = 2)]
public String Units { get; set; }
public MyClass(DateTime interval, double actual, string units)
{
this.Interval = interval;
this.Actual = actual;
this.Units = units;
}
}
By the way it did not solve my problem fully either. I needed to call DataContractSerializer to serialize/sequence/order the data before sending.
public List GetData(string token)
{
List data = null;
foreach (DataRow dr in dt.Rows)
{
MyClass points = new MyClass(param1,param2,param3);
DataContractSerializer contractSerializer = new DataContractSerializer(typeof(MyClass));
data.Add(points);
index++;
}
}
return data;
}
This post describes this too.
Leave a comment