0
votes

I'm trying to set SignalR in my MVC4 app.

The problem is - even though when I browse to path /signalr/hubs I do see code (and fiddler shows 200OK for /signalr/hubs), it does not seem to contain any reference to my hub and client side code also doesn't see the hub and methods.

I get these errors when starting debugging (IIS Express, VS Express 2012):

Object has no method sendingcannot set property 'messageAll' of undefined

Application_Start in Global.asax contains:

        //RouteTable.Routes.MapHubs("/signalr", new HubConfiguration());
        RouteTable.Routes.MapHubs();
        RouteConfig.RegisterRoutes(RouteTable.Routes);

(I assume this generates the /signalr/hubs, this seems to work but nothing links to my actual hub. As can be seen I tried both options).

In my project I got folder "Hubs" in root with MessageHub.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace Prj.Hubs 
{
[HubName("messagehub")]
public class MessageHub : Hub
{
    public void MessageAll(string message)
    {
        Clients.All.writeMessage(message);
    }

    public void MessageOthers(string message)
    {
        Clients.Others.writeMessage(message);
    }

    public void MessageSingle(string message)
    {
        
    }
}

}

In my _Layout.cshtml I have just before the closing tag:

<script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery.signalR-1.1.2.js"></script>
    <script src="/signalr/hubs" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            // create proxy on the fly
            var proxy = $.connection.messagehub; // this connects to our 'messageHub' Hub as above

            // for SignalR to call the client side function we need to declare it with the Hub
            proxy.messageAll = function (message) {
                $('#messages').append('<li>' + message + ''); // when the Hub calls this function it appends a new li item with the text 
            };

            // declare function to be called when button is clicked 
            $("#broadcast").click(function () {
                // calls method on Hub and pass through text from textbox 
                proxy.messageAll($("#message").val());
            });

            // Start the connection 
            $.connection.hub.start();
        });
    </script>

(side note - SignalR didn't like at all the @Scripts.Render("~/bundles/jquery"), but a direct jquery script include seems to work).

So why doesn't it recognize "messagehub" exactly?

1

1 Answers

1
votes

I solved this - my solution contains multiple projects, and although I had uninstalled SignalR, in some of these projects in the bin/Debug folder there were still traces of an older SignalR version that I tried months ago.

At runtime, SignalR was trying to connect some old dll's with the new references. So if you have this error then

  • uninstall SignalR
  • do a search for "SignalR" in your entire solution folder and delete everything
  • reinstall SignalR from Nuget Package Manager.