【发布时间】:2016-09-23 16:12:43
【问题描述】:
我有以下异常不一致但经常生成:
System.Exception: Serialization error - One or more errors occurred., Unexpected character encountered while parsing value: <. Path '', line 0, position 0., System.AggregateException: One or more errors occurred. ---> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b__1(String raw)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.b__17(Task`1 t)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners`2.<>c__DisplayClass42.b__41(Task`1 t)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
**at Project.Services.NotificationService.SignalObjectToUser(Object data, String username, Boolean isTypingNotification)
---> (Inner Exception #0) Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.**
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b__1(String raw)
at Microsoft.AspNet.SignalR.TaskAsyncHelper.<>c__DisplayClass19`2.b__17(Task`1 t)
重要的一点:
**at Project.Services.NotificationService.SignalObjectToUser(Object data, String username, Boolean isTypingNotification)
---> (Inner Exception #0) Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.**
我的代码:
private void SignalObjectToUser(object data, string username, bool isTypingNotification = false) {
try {
_hubConnection = new HubConnection(_baseUrl);
_hubConnection.CookieContainer = new CookieContainer();
_hubConnection.CookieContainer.Add(_cookie);
_hubProxy = _hubConnection.CreateHubProxy("appHub");
_hubConnection.Start().Wait();
string serialized = null;
try {
List<string> errors = new List<string>();
serialized = JsonConvert.SerializeObject(data, new JsonSerializerSettings() {
Error = delegate(object sender, ErrorEventArgs args) {
errors.Add(args.ErrorContext.Error.Message);
args.ErrorContext.Handled = true;
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Delegated serialization error - " + args.ErrorContext.Error.Message));
}
});
_hubProxy.Invoke((isTypingNotification ? "SendTypingNotification" : "SendClientNotification"), serialized, username).Wait();
}
catch (Exception exception) {
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Serialization error - " + exception.Message + ", " + exception.InnerException.Message + ", " + exception.ToString()));
throw new Exception("Messaging Error");
}
}
catch (Exception e) {
Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Serialization error - " + e.Message + ", " + e.InnerException.Message + ", " + e.ToString()));
throw new Exception("Messaging Error");
}
}
我正在尝试找出 究竟是什么 导致了这个问题。我不知道是什么键/值对产生了异常。我怎样才能在这里收集更多有启发性的细节?
【问题讨论】:
-
错误表示意外字符是
<在第 0 行位置 0。您是否将 XML 传递给 JSON 反序列化器?如果是这样,那就是问题所在。有效的 JSON永远不会以<开头 -
@BrianRogers 似乎我不知何故,但我不知道如何或何时。我希望有一种预序列化的方法,以便我可以看到序列化问题,哈!
标签: .net serialization exception-handling json.net elmah