I have implemented a WCF Data Services service like this:
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class SampleService : DataService<SampleDatabase>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("SampleData", EntitySetRights.All);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
}
However, as has been documented elsewhere, Dates are not correctly formatted by the framework JSON Serialization code. The date is not output in a valid ISO format. What's more astonishing is that WCF doesn't round-trip the date that is output. So it outputs a date that looks like this: "/Date(1325376000000)/", but does not accept that same value as a valid date in return.
Hanselman has how to fix it for WebAPI: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
And I've found similar fixes for MVC serialization: http://www.dalsoft.co.uk/blog/index.php/2012/01/10/asp-net-mvc-3-improved-jsonvalueproviderfactory-using-json-net/
Our client has specified MVC3 and .NET 4, so I think WebAPI is out, so I would like to solve this for MVC3 + WCF Data Services as found in .NET 4. My understanding from the above fixes is that the best fix is to use JSON.NET to override the serialization/deserialization to an ISO format both .NET and Javascript understand. But I'm unable to find any guidance on overriding the JSON serialization for this.
How can this problem be fixed specifically for WCF Data Services in .NET 4?
In OData V3 date time in Verbose JSON is written using the ISO format as well. For this to work you will have to install the WCF Data Services 5.0 (which implements OData V3), allow V3 on the server and modify clients to request V3 (since V2 payload will have the old format for backward compatibility). To force usage of V3 payload format, the client can send MinDataServiceVersion: 3.0; header.
Note: The old format actually does round trip, but it uses tricky JSON escaping. The actual format is \/Date(12345678)\/
(Which is semantically the same /Date(12345678)/
, but on the wire it looks differently). Unfortunately such string cannot be produced by the JSON serialization code in browsers.