最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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.

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).

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="">
<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:

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"

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

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

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...

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.

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.

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).

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/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:

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"

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

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

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...

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.

Share Improve this question edited Aug 13, 2013 at 17:55 user2676522 asked Aug 12, 2013 at 21:41 user2676522user2676522 511 gold badge1 silver badge4 bronze badges 7
  • This is tough, you can elaborate on the contrast between the setup that works on IIS and the one that doesn't. You said, "as a new web application running as a web app under the existing web application, everything works fine". How does this differ from the setup on IIS that signalR fails on? – Brian Ogden Commented Aug 12, 2013 at 22:34
  • Sure, I can try... In IIS, I have a Web Application called "MyWebApplication". It is a large and plicated Web Application that has been around for awhile. I get to my test page by going to mypany./mytest/signalrtest.aspx. For some reason, my hub proxy is not being created in this project. If I create a new Visual Studio project, and call it mytest2 that contains only the signalr code (hub and client page), I deploy the project as an app under my main application in IIS (so now I go to mypany./mytest/mychildtest/signalrtest.aspx). Now my hub proxy generates ok – user2676522 Commented Aug 12, 2013 at 22:42
  • Perhaps "MyWebApplication" has some security settings that have locked down some of the needed ASP.NET libraries Signalr requires to work correctly. Do you have any application logging in place such as Elmah or Enterprise Library? Have you checked the Windows Application Event Log just in case? – Brian Ogden Commented Aug 12, 2013 at 22:50
  • The error is a javascript one. However SignalR creates the hub proxy does not seem to be working in my case. I just wish I know more about why. Near as I can tell it is the RouteTable.Routes.MapHubs() in the Global.asax that creates the proxy, and that runs without errors. How can I check the SignalR hub proxy is created beyond trying to get it in javascript? – user2676522 Commented Aug 12, 2013 at 22:58
  • Could be worth running Fiddler over the request to the remote web server to see if there are any script load errors. That could help narrow the search. – SteveChapman Commented Aug 12, 2013 at 23:08
 |  Show 2 more ments

1 Answer 1

Reset to default 4

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.

To solve the issue I had to add the virtual directory name ("TestingChat") to the path of the script tags for jquery and 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>
发布评论

评论列表(0)

  1. 暂无评论