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

4
以下是我在两个不同页面上使用的客户端代码:
<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>

在一个页面上它连接并正常工作,但在另一个页面上它给我以下错误:
[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

我看到的唯一区别是: 在调用 negotiate utl 时,“connectionData”参数不同: 对于正常工作的页面,它是这样的:
connectionData  [{"name":"notificationhub"}]

对于那个无法正常工作的页面,看起来像这样:
connectionData  "[{\"name\": \"notificationhub\"}]"

有人能否建议我到底缺少了什么或者应该检查什么?

你找到解决方案了吗? - Armand
你打开这两个页面时是否会出现错误,还是总是有一个特定的页面失败? - Shashank Chaturvedi
我得到了相同的结果。每当connectionData被双重转义时,它都会失败。 - joe_coolish
1个回答

5

好的,我之所以会出现这个错误,是因为:

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

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

走进函数调用,看看你的JSON.stringify方法是否已被覆盖。在我的情况下,我使用的是旧版本的prototype.js,它覆盖了JSON.stringify函数,导致解析不正确。

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

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

如果应该返回"[{"name":"testhub"}]",如果它返回""[{\"name\": \"testhub\"}]"",那么您的stringify函数已经被破坏了。


首先,我正在使用SignalR 1.2.0;其余的我正在检查,并将发布我的发现。谢谢。 - marifrahman
无论是哪个版本的SignalR,它都应该是相同的。如果这解决了你的问题,请告诉我! - joe_coolish
1
是的,发生了这种情况 - prototypejs 的 toJson 覆盖了 JSON.stringify();为了强制不覆盖,这里提供了解决方案:https://dev59.com/t3RB5IYBdhLWcg3wH0WW - marifrahman
1
哈哈!两年后,我又遇到了这个问题!忘记了解决方法,找到了这个页面,试图给发帖人点赞,然后意识到那个人是我自己...老了真不容易。 - joe_coolish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接