Wednesday 6 June 2012

WCF Data Services - non-unique DataServiceKey attribute causes strange results

I had a data service that was returning an IQueryable of a custom type:

[WebGet]
public IQueryable<VolumeEntry> GetTradeVolumes(int tranNum)

[DataServiceKey("StartTimeUtc")]
public class VolumeEntry
{
    public DateTime StartTimeUtc { getset; }
    public DateTime EndTimeUtc { getset; }
    public int VolumeType { getset; }
    public double Quantity { getset; }
}

The server was sending the data in the correct form however the client was returning the wrong results. It returned the correct number of results but the VolumeType and Quantity fields were wrong - they always contained the same values.

The problem was that the custom type was actually keyed on StartTimeUtc AND VolumeType, not just StartTimeUtc as was marked with the DataServiceKey attribute.

Once modified the client returned the right results.

[DataServiceKey("StartTimeUtc""VolumeType")]
public class VolumeEntry
{
    public DateTime StartTimeUtc { get; set; }
    public DateTime EndTimeUtc { get; set; }
    public int VolumeType { get; set; }
    public double Quantity { get; set; }
}

No comments:

Post a Comment