Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 676
user2676522 Avatar asked Oct 04 '22 06:10

user2676522


1 Answers

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>
like image 132
CristisS Avatar answered Oct 13 '22 11:10

CristisS