I am trying to send request from the server to client(.Net) which is running in culture specific environment which affects dates to be in special time zone.
I am sending date from the server with Unspecified kind for avoiding it being converted while getting retrieved on client side. Basically I just convert it like this on server side:
DateTime dateToSend = DateTime.SpecifyKind(serverSideDate, DateTimeKind.Unspecified);
The problem is that when using JsonProtocol for the client, date is not getting converted and is getting processed correctly, while for MessagePackProtocol the same code for client and server side works completely different way - it converts date to culture specific timezone on the client...
How to prevent that conversion, without some hack solutions like passing date as a string.
UPDATE:
As Shaun suggested, I've configured MessagePack both on client and server side this way, but unfortunaly it's still not working:
.AddMessagePackProtocol(options =>
options.FormatterResolvers = new List<MessagePack.IFormatterResolver>()
{
StandardResolver.Instance,
NativeDateTimeResolver.Instance
})
The problem is that kind is lost with the MessagePackProtocol.
DateTime
is serialized to MessagePack Timestamp format, it serialize/deserialize UTC and losesKind
info.
The note continues by saying we can change that by using the NativeDateTimeResolver
instead (albeit with some limitations).
If you use
NativeDateTimeResolver
serialized nativeDateTime
binary format and it can keepKind
info but cannot communicate other platforms.
As Chris and Panagiotis mention, it's better to use DateTimeOffset
. The official Microsoft documentation says this:
DateTimeOffset should be considered the default date and time type for application development.
There is also much useful information on StackOverflow comparing DateTime vs DateTimeOffset.