我使用SignalR将一个对象从客户端发布到服务器,该对象包含一个date属性,有时候有一个UTC DateTime,有时候还有一个LOCAL DateTime(这本身就是一个单独的问题)。我注意到的是,只要日期是当地时间,例如'2013-01-08T10:35:59.8484157 + 00:00'我得到'无法将字符串转换为DateTime'错误。在检查时,似乎尝试解析的字符串已从时区偏移信息中丢失了“+”字符,这导致转换错误。我试图了解这个角色是如何丢失的。
SignalR正确地将对象序列化为json并对其进行编码,因为+字符编码为%2B,可以在SignalR请求的正文中看到
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
通过UrlDecode函数运行此函数会返回正确的值
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
}
但是当查看SignalR请求上下文时,正文列出了'+',现在是一个空格。这会导致日期转换错误。似乎SignalR / JSON.NET / .NET(不确定哪一个)的某个地方是对发布的数据进行双重解码并将+转换为空格。
我创建了一个简单的MVC应用程序来演示这个问题,它在GitHub上http://github.com/davidthompson/SignalRPlusEncodingTest
示例应用程序运行后,单击任何具有UTC日期的警报旁边的确认按钮,这将在集线器上调用SignalR方法,警报将消失。如果对具有本地日期的警报执行相同的操作,它将失败(单击“创建新警报”按钮,它将随机生成具有UTC或LOCAL日期的警报)。浏览器控制台将显示有关转换失败的错误。但如果没有收到的错误如下:
[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)
提前致谢