Azure Mobile Services and DateTimeOffset

Important update: Please, see a comprehensive post by Carlos Figueira that recommends two additional workarounds for this issue.

I ended up implementing a combination of my approach below and Carlos’ custom JsonConverter.

I use DateTimeOffset very extensively, my corresponding SQL Azure table in mobile services uses datetimeoffset(3) column. The issue that I had was somewhere during JSON.NET serialization, the DateTimeOffset lost the offset and got converted to UTC. I couldn’t find any solution, until I experimentally started playing with the Mobile Services Client SerializationSettings, exposed in the latest SDK. Changing DateTimeZoneHandling  didn’t seem to help much until I discovered this post by Carlos. I changed it a little bit, keeping the idea, and voila: my DateTimeOffset now saves in Azure Mobile Services, no problem. If you know a better way, please let me know:

 

                MobileService.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.RoundtripKind;
                // remove date time converter
                var conv = MobileService.SerializerSettings.Converters.Where(c => c is MobileServiceIsoDateTimeConverter).FirstOrDefault();
                if (conv != null)
                {
                    MobileService.SerializerSettings.Converters.Remove(conv);
                }
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *


+ four = 7

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>