0

I have use case where the a local Agent sends date time to backend WCF service as JSON. The date time serialized by the agent looks like this

Date(1452450600000+0530)

The WCF service acts as a virtualization layer by accepting a DTO and serializing it to a disk to be processed asynchronously.

However when the JSON gets deserialized in WCF service which is running unders PST time, the JSON loses the local time information and the time is represented in PST. ie, +5:30 is replaced by -8:00 with the time change correspondingly.

The problem is that Agents are installed on system operation in multiple timezones and it is important for the system to maintain the timezone information till the end.

Is it possible to keep the client date time intact in WCF serviceand prevent the conversion to PST

Serialization method is

if (obj == null) return null; Type type = obj.GetType(); var ser = new DataContractJsonSerializer(type); using (var ms = new MemoryStream()) { ser.WriteObject(ms, obj); return Encoding.UTF8.GetString(ms.ToArray()); }

Ranjeeth
  • 91
  • 1
  • 9
  • 1
    I would suggest you do not use .net DateTime for this. Try Jon Skeet's time library NodaTime and [try this question on how to use Noda with WCF](http://stackoverflow.com/questions/18632361/how-to-pass-a-noda-time-or-any-third-party-type-object-as-a-parameter-in-wcf) – Aron Jan 11 '16 at 07:37
  • Actually the issue seems to be your JSON serializer. Please show how you are configuring JSON serialization, possibly on the client end. – Aron Jan 11 '16 at 07:40
  • Aron - First of all thanks for the response. Here is a code as is on server side. public T Deserialize(string objStr) { if (String.IsNullOrEmpty(objStr)) return default(T); using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(objStr))) { var ser = new DataContractJsonSerializer(typeof(T)); return (T)ser.ReadObject(ms); } } – Ranjeeth Jan 11 '16 at 07:49
  • Forgot to mention the same helper class is used on both client ans server side if (obj == null) return null; Type type = obj.GetType(); var ser = new DataContractJsonSerializer(type); using (var ms = new MemoryStream()) { ser.WriteObject(ms, obj); return Encoding.UTF8.GetString(ms.ToArray()); } – Ranjeeth Jan 11 '16 at 09:44
  • please could you write those "comments" into the actual question. It is very hard for us to read code in comments, and not all comments are visible straight away. – Aron Jan 11 '16 at 10:20
  • One of the way I tried getting over the problem is to pass the offset along with my DTO. This way i can ensure that whichever zone the datetime gets serialized to does not matter. Finally I convert the datetime to UTC and apply the offset to get the original value back – Ranjeeth Jan 15 '16 at 15:10

0 Answers0