SignalR:无法从System.String转换或转换为System.Collections.Generic.IEnumerable

mar*_*man 4 asp.net jquery json signalr

以下是我在两个不同页面上使用的客户端代码:

<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>
Run Code Online (Sandbox Code Playgroud)

在一个页面上它连接并正常工作,但在另一个页面上它给我以下错误:

[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
Run Code Online (Sandbox Code Playgroud)

我看到的唯一区别是:在调用negotiate时,'connectionData'参数不同:对于其工作方式的页面:

connectionData  [{"name":"notificationhub"}]
Run Code Online (Sandbox Code Playgroud)

而对于它不工作的页面看起来像:

connectionData  "[{\"name\": \"notificationhub\"}]"
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我实际上缺少什么或检查什么?

joe*_*ish 5

好的,这就是我收到此错误的原因.

jquery.signalr-2.0.0.js文件中,在第2578行设置断点

connection.data = connection.json.stringify(subscribedHubs);
Run Code Online (Sandbox Code Playgroud)

进入该函数调用,看看您的JSON.stringify方法是否已被覆盖.在我的情况下,我使用的旧版本prototype.js覆盖了该JSON.stringify函数,导致错误的解析.

另外,尝试从F12开发人员工具运行以下命令:

JSON.stringify([{name:"testhub"}]);
Run Code Online (Sandbox Code Playgroud)

如果应该返回"[{"name":"testhub"}]",如果它返回""[{\"name\": \"testhub\"}]""你的stringify已被泄露.