【问题标题】:SignalR : Could not cast or convert from System.String to System.Collections.Generic.IEnumerableSignalR:无法从 System.String 转换或转换为 System.Collections.Generic.IEnumerable
【发布时间】:2014-02-17 02:47:22
【问题描述】:

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

<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

我看到的唯一区别是: 在调用协商 utl 时,“connectionData”参数是不同的: 对于其工作方式如下的页面:

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

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

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

谁能建议我实际上缺少什么或应该检查什么?

【问题讨论】:

  • 您找到解决方案了吗?
  • 是连续打开两个页面失败还是某个特定页面一直都失败?
  • 我得到了同样的结果。每次连接数据被双重转义时都会失败

标签: jquery asp.net json signalr


【解决方案1】:

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

在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;其余的我正在检查并将发布我的发现。不过谢谢..
  • 无论 SignalR 版本如何,都应该相同。让我知道它是否可以为您解决问题!
  • 是的。这就是发生的事情——prototypejs toJson 覆盖 JSON.stringyfy() ;强制不要在这里覆盖是解决方案:stackoverflow.com/questions/710586/json-stringify-bizarreness
  • 哈哈! 2年后,我又遇到了这个问题!忘记了修复,找到了这个页面,试图给海报点个赞,然后意识到是我……男人变老是粗糙的
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
相关资源
最近更新 更多