4

Following is my client code that I am using on two different pages:

<script src='<%= Page.ResolveUrl("~/resources/js/jquery-1.6.4.min.js") %>' type="text/javascript"></script>re
    <script type="text/javascript">
        jQuery.noConflict();
    </script>
    <script src='<%= Page.ResolveUrl("~/resources/js/jquery.signalR-1.2.0.min.js") %>' type="text/javascript"></script>
    <script src='<%= Page.ResolveUrl("~/signalr/hubs") %>' type="text/javascript"></script>
    <script type="text/javascript">
        jQuery(function () {
            // Declare a proxy to reference the hub. 
            var usr = jQuery.connection.notificationHub;
            jQuery.connection.hub.logging = true;

            jQuery.connection.hub.qs = "clientId=arif";// +readCookie("personId");

            jQuery.connection.hub.start().done(function () {

            });
            usr.client.showUsersOnLine = function (data) {
            };

        });

    </script>

On one page it gets connected and is working ok, but on another page its giving me following error :

[ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.IEnumerable`1[Microsoft.AspNet.SignalR.Hubs.HubDispatcher+ClientHubInfo].]
   Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) +241
   Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) +123
   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) +238

[JsonSerializationException: Error converting value "[{"name": "notificationhub"}]" to type 'System.Collections.Generic.IEnumerable`1[Microsoft.AspNet.SignalR.Hubs.HubDispatcher+ClientHubInfo]'. Path '', line 1, position 35.]
   Microsoft.Owin.Host.SystemWeb.Infrastructure.<>c__DisplayClass1.<GetRethrowWithNoStackLossDelegate>b__0(Exception ex) +27
   Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +34
   Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult result) +49
   Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.EndProcessRequest(IAsyncResult result) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9628972
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

The only difference I see is : While calling the the negotiate utl the 'connectionData' param is different : For the page where its working its like :

connectionData  [{"name":"notificationhub"}]

And for the page where it's not working looks like :

connectionData  "[{\"name\": \"notificationhub\"}]"

Can anybody please suggest what am I actually missing or what should the check ?

marifrahman
  • 681
  • 2
  • 13
  • 31

1 Answers1

5

Ok, this is why I was getting this error.

In the jquery.signalr-2.0.0.js file, set a breakpoint at line 2578

connection.data = connection.json.stringify(subscribedHubs);

Step into that function call and see if your JSON.stringify method has been overridden. In my case, I was using an older version of prototype.js that was overriding the JSON.stringify function, causing the wrong parsing to occur.

Also, try running the following command from the F12 Developer Tools:

JSON.stringify([{name:"testhub"}]);

if should return "[{"name":"testhub"}]", if it returns ""[{\"name\": \"testhub\"}]"" your stringify has been compromised.

joe_coolish
  • 7,201
  • 13
  • 64
  • 111
  • First, I am using SignalR 1.2.0; The rest I am checking and will post my findings. thnx though.. – marifrahman Feb 21 '14 at 00:07
  • It should be the same regardless of the SignalR version. Let me know if it fixes the problem for you! – joe_coolish Feb 21 '14 at 02:11
  • 1
    Yes. That's what happened - prototypejs toJson overrides JSON.stringyfy() ; to force not to override here is the solution :http://stackoverflow.com/questions/710586/json-stringify-bizarreness – marifrahman Feb 21 '14 at 06:19
  • 1
    LOL! 2 years later, I had this issue again! Forgot the fix, found this page, tried to upvote the poster, then realized it was ME... Man getting old is rough – joe_coolish Jun 13 '16 at 12:41