javascript SignalR - 无法获取未定义或空引用的属性“客户端”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18197134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:03:08  来源:igfitidea点击:

SignalR - Unable to get property 'client' of undefined or null reference

javascriptjquery.netiissignalr

提问by user2676522

I am having problems using SignalR in an existing Web Application. I am receiving the javaruntime exception "Unable to get property 'client' of undefined or null reference" because when I try to retrieve my hub from the $.connection object, it is returning null.

我在现有 Web 应用程序中使用 SignalR 时遇到问题。我收到 javaruntime 异常“无法获取未定义或空引用的属性‘客户端’”,因为当我尝试从 $.connection 对象检索我的集线器时,它返回空值。

Before I post my code, I wanted to supply some background and some things I have already tried: - If I run the project locally, everything works fine. It is only when I deploy my project to the IIS location that I have issues. - I have verified my hub name is correctly camel cased. Again, it works fine locally, and only throws the error after I have deployed the project to IIS. - I have confirmed that all my javascript has successfully loaded. The JQuery library is only referenced once, and loads fine. So does the signalR javascript, and the dynamically created signalr/hubs. - If I create a new project, and run the same code as a new web application running as a web app under the existing web application, everything works fine (same code is being used, just in a new project).

在发布我的代码之前,我想提供一些背景知识和一些我已经尝试过的东西: - 如果我在本地运行该项目,则一切正常。只有当我将项目部署到 IIS 位置时才会出现问题。- 我已经确认我的集线器名称是正确的驼峰式大小写。同样,它在本地工作正常,只有在我将项目部署到 IIS 后才会抛出错误。- 我已经确认我所有的 javascript 都已成功加载。JQuery 库只被引用一次,并且加载正常。signalR javascript 和动态创建的信号器/集线器也是如此。- 如果我创建一个新项目,并运行与在现有 Web 应用程序下作为 Web 应用程序运行的新 Web 应用程序相同的代码,一切正常(正在使用相同的代码,只是在新项目中)。

Anyone have any thoughts on what the problem is?

任何人对问题是什么有任何想法?

Hub Code:

枢纽代码:

Imports Microsoft.AspNet.SignalR

Public Class TestHub
    Inherits Hub

    Public Sub Connect()
        Try
            Clients.Caller.onConnect(Context.ConnectionId)
        Catch ex As Exception
            LogMessage("ERROR Connecting to Hub", 0, 0, "ERROR")
        End Try
    End Sub

End Class

Web Page Code:

网页代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.6.4.min.js"></script>
    <!--Reference the SignalR library. -->
    <script src="Scripts/jquery.signalR-1.1.3.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="signalr/hubs"></script>
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub. 
            var thub = $.connection.testHub;

            registerClientMethods(thub);

            // Start Hub
            $.connection.hub.start().done(function () {
                registerEvents(thub);
                thub.server.connect();
                alert('connected!');
            });
        });


        function registerClientMethods(thub) {
            thub.client.onConnect = function (id) {
                $('#lblConnectionID').text(id);
            }
        }

        function registerEvents() {

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblConnectionID" runat="server" />
    </div>
    </form>
</body>
</html>

Global.asax Code:

Global.asax 代码:

Imports System.Web.Routing
Imports Microsoft.AspNet.SignalR

Public Class Global_asax
  Inherits System.Web.HttpApplication
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the application is started
    Try
    ' Register the default hubs route: ~/signalr/hubs
    RouteTable.Routes.MapHubs()
    Catch ex As Exception
    LogMessage("ERROR initializing SignalR: " & ex.Message, 0, 0, "ERROR")
    End Try
End Sub
...

So now, when I go to my page SignalRTest.aspx, the line thub.client.onConnected = ...throws a javascript error: "Unable to get property 'client' of undefined or null reference"

所以现在,当我转到我的页面 SignalRTest.aspx 时,thub.client.onConnected = ... 行抛出一个 javascript 错误:“无法获取未定义或空引用的属性‘客户端’”

Does Anyone know what might be causing this? Thank you.

有谁知道这可能是什么原因造成的?谢谢你。

[EDIT] - After comparing the working signalr/hubs.js file with the non-working file, I found the following piece missing:

[编辑] - 将工作的 signalr/hubs.js 文件与非工作文件进行比较后,我发现以下部分丢失:

proxies.testHub= this.createHubProxy('testHub'); 
    proxies.testHub.client = { };
    proxies.testHub.server = {
        connect: function () {
            return proxies.transactionHub.invoke.apply(proxies.testHub, $.merge(["Connect"], $.makeArray(arguments)));
         }
    };

Any thoughts on why this would not get included in the signalr/hubs.js file? I am calling the MapHubs() in Global.asax...

关于为什么这不会包含在 signalr/hubs.js 文件中的任何想法?我在 Global.asax 中调用 MapHubs() ...

Thanks!

谢谢!

[FIX]I deleted the Bin folder of my destination web application, and redeployed. Still not sure what was different. All the .DLLs are the same. It now works though. Odd.

[修复]我删除了目标 Web 应用程序的 Bin 文件夹,然后重新部署。仍然不确定有什么不同。所有的 .DLL 都是一样的。不过现在它可以工作了。奇怪的。

回答by CristisS

I encountered this error when trying to debug to run a web application after publishing from VS 2012 to IIS 7.5. After some hair pulling, I realised that the problem was the fact that I did not change the jquery script and signalr paths when deploying.

我在从 VS 2012 发布到 IIS 7.5 后尝试调试以运行 Web 应用程序时遇到此错误。经过一番拉扯,我意识到问题在于我在部署时没有更改 jquery 脚本和信号器路径。

To solve the issue I had to add the virtual directory name ("TestingChat") to the path of the script tags for jquery and signalr.

为了解决这个问题,我必须将虚拟目录名称(“TestingChat”)添加到 jquery 和 signalr 的脚本标签的路径中。

E.g. From this:

例如从这个:

<script src="Scripts/jquery-1.8.2.min.js" ></script>
<script src="/signalr/hubs"></script>

To

<script src="/TestingChat/Scripts/jquery-1.8.2.min.js" ></script>
<script src="TestingChat/signalr/hubs"></script>