I am using SignalR to post an object from the client to the server, the object contains a date property which sometimes has a UTC DateTime and sometimes a LOCAL DateTime (this is a separate issue in itself). What I have noticed is that whenever the date is a local time e.g. ‘2013-01-08T10:35:59.8484157+00:00’ I get a ‘could not convert string to DateTime’ error. Upon inspection it appears that the string which is trying to be parsed has lost the ‘+’ character from the time zone offset information which causes the conversion error. I am trying to understand how this character has been lost.
SignalR is correctly serializing the object to json, and encoding it, as the + character is encoded as %2B as can be seen in the body of the SignalR request
data=%7B%22H%22%3A%22alertshub%22%2C%22M%22%3A%22AcknowledgeAlert%22%2C%22A%22%3A%5B%7B%22Id%22%3A%22e2d65f79-6a03-4094-a00f-99fcd9b46a7a%22%2C%22StartDateTimeUtc%22%3A%222013-01-08T10%3A26%3A44.0849463%2B00%3A00%22%2C%22EndDateTimeUtc%22%3Anull%2C%22AcknowledgedDateTimeUtc%22%3Anull%2C%22Description%22%3A%22Test+Alert+17%22%7D%5D%2C%22I%22%3A0%7D
Running this via a UrlDecode function returns the correct values
data={
"H":"alertshub",
"M":"AcknowledgeAlert",
"A":[{
"Id":"e2d65f79-6a03-4094-a00f-99fcd9b46a7a",
"StartDateTimeUtc":"2013-01-08T10:26:44.0849463+00:00",
"EndDateTimeUtc":null,
"AcknowledgedDateTimeUtc":null,
"Description":"Test Alert 17"
}],
"I":0
}
But when looking at the SignalR request context the body has list the ‘+’, and is now a space. This causes the date conversion error. It appears that somewhere along the line SignalR/JSON.NET/.NET (not sure which one) is double decoding the posted data and turning the + into a space.
I have created a simple MVC application which demonstrates the issue, its up on GitHub http://github.com/davidthompson/SignalRPlusEncodingTest
Once the sample application is running click the acknowledge button next to any alerts with a UTC date it, this will call a SignalR method on the hub and the alert will disappear. If you perform the same action against an alert with a local date it will fail (click ‘Create new alert’ button, it will randomly generate an alert with either a UTC or LOCAL date). The browser console will show the error about the failed conversion. But in case that doesn’t the error received is below:
[10:36:04 GMT+0000 (GMT Standard Time)] SignalR: Could not convert string to DateTime: 2013-01-08T10:35:53.1401147 00:00. Path 'StartDateTimeUtc', line 1, position 144.
at Newtonsoft.Json.JsonReader.ReadAsDateTimeInternal()
at Newtonsoft.Json.Linq.JTokenReader.ReadAsDateTime()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Microsoft.AspNet.SignalR.JTokenValue.ConvertTo(Type type)
at Microsoft.AspNet.SignalR.Hubs.DefaultParameterResolver.ResolveParameter(ParameterDescriptor descriptor, IJsonValue value)
at System.Linq.Enumerable.<ZipIterator>d__7a`3.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Microsoft.AspNet.SignalR.Hubs.DefaultParameterResolver.ResolveMethodParameters(MethodDescriptor method, IJsonValue[] values)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.InvokeHubPipeline(IHub hub, IJsonValue[] parameterValues, MethodDescriptor methodDescriptor, HubRequest hubRequest, StateChangeTracker tracker)
jquery....min.js (line 10)
Thanks in advance
This was fixed in the latest dev branch of SignalR via issue https://github.com/SignalR/SignalR/issues/1194.