1

I have the following code:

 try {
            _hubConnection = new HubConnection(_baseUrl);
            _hubConnection.CookieContainer = new CookieContainer();
            _hubConnection.CookieContainer.Add(_cookie);
            _hubProxy = _hubConnection.CreateHubProxy("appHub");
            _hubConnection.Start().Wait();
        }
 catch (Exception e) {
            Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Serialization error [hubConnection] - " + e.Message + ", " + e.InnerException.Message + ", " + e.ToString()));
        }

Usually, this works. Every once in a while, I get a random serialization error though:

Serialization error [hubConnection] - 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 

This Unexpected character encountered while parsing value: < seems very difficult to reproduce, and I don't know how to get detailed information around this. I do have this configured:

        var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true };
        app.MapSignalR(hubConfiguration);

Does anyone know what might be causing this, or how I might go about troubleshooting?

Full trace:

System.Exception: Messaging Error [hubConnection]    at App.Services.NotificationService.SignalObjectToUser(Object data, String username, Boolean isTypingNotification)    at App.Services.NotificationService.MessageSentNotification(User toUser, Message message, Int32 conversationId, Boolean isOnline)    at App.Services.MessageService.SendMessage(MessageDto message, FStopContext altContext)    at App.ApiControllers.MessageController.SendMessage(MessageDto message) at lambda_method(Closure , Object , Object[] )    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)    at System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()
RobVious
  • 12,685
  • 25
  • 99
  • 181
  • The error message **Unexpected character encountered while parsing value: <. Path '', line 0, position 0.** almost certainly means that XML was received instead of JSON. – dbc Jun 11 '16 at 23:20
  • @dbc From where and why though? – RobVious Jun 12 '16 at 00:36
  • This I don't know -- that's why it's a comment not an answer. Maybe an exception thrown on the server side? – dbc Jun 12 '16 at 00:44
  • @dbc - this is serverside and the only exception thrown gives me the details I pasted above. That is thrown from the `catch`. – RobVious Jun 12 '16 at 00:50
  • I reckon it would be useful to find out exactly what XML is being sent. However, I don't know SignalR well. 1) That traceback seems incomplete. Can you share the full traceback? 2) Is there any way to get access to the `JsonTextReader` being used to deserialize the current object, similar to how the `JsonTextWriter` is made available [here](https://stackoverflow.com/questions/37448604)? 3) If worst came to worst, could you build your own version of Json.NET and deploy it on the server? – dbc Jun 12 '16 at 16:45
  • @dbc full trace pasted. Seeing the XML would be amazing - but I just don't know how :(. Obviously I'd rather not build another deserializer. There HAS to be a way to gain verbosity here. – RobVious Jun 12 '16 at 17:37
  • You don't have the full traceback that includes the call to Json.NET, do you? It looks like the first traceback you posted (the one that includes `JsonSerializerInternalReader.ReadForType`) has been truncated. – dbc Jun 12 '16 at 18:30
  • @dbc - thanks for the continued help here. This is just the full exception thrown by .NET - how can I get that for you? I don't believe I truncated anything. – RobVious Jun 12 '16 at 19:36
  • To me it looks like its getting ahead of itself, its trying to send a call through the hub that has not yet had its message created. I would look for situations where an empty message is being sent – Kelso Sharp Jun 13 '16 at 15:02
  • Can you enable tracing on the client side : `hubConnection.TraceLevel = TraceLevels.All; hubConnection.TraceWriter = Console.Out;`. Reference : http://www.asp.net/signalr/overview/testing-and-debugging/enabling-signalr-tracing – Quentin Roger Jun 14 '16 at 09:00

1 Answers1

1

There are a couple of things I would try before digging deeper into the content being transmitted:

  1. Set up the hub connection without a cookie container. This would narrow down whether this is causing the issue.
  2. Rather than using .Wait() on your hub connection, try use an async/await function. I seem to recall having a similar exception when I was using Wait() in the past.

Post back your results.

aleksk
  • 661
  • 7
  • 15