0
votes

I am new to SignalR and having a bit of difficulty using a SignalR hub in a self hosted scenario. At the moment (just for testing) I have the simplest hub possible:

public class NotificationsHub : Hub
    {
        public void Hello(string name)
        {
            Clients.All.hello("Hello " + name);
        }        
    }

This hub class is in a Class Library project which is referenced in a Windows Service application. I've added all the nuget packages to the Windows Service app and added the OWIN Startup class which looks like this:

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {            
            AppDomain.CurrentDomain.Load(typeof(NotificationsHub).Assembly.FullName); // as selfhosting doesn't scan referenced libraries for Hubs
            app.Map("/signalr", map => {
                map.UseCors(CorsOptions.AllowAll);
                var hubConfig = new HubConfiguration { 
                    EnableDetailedErrors = true,
                    EnableJSONP = true
                };
                map.RunSignalR(hubConfig);
            });
        }
    }

In the Windows Service OnStart method I host the signalR using:

SignalR = WebApp.Start<Startup>("http://*:9191/");

In the ASP.NET MVC app which should interact with SignalR I have the following code:

<script src="Scripts/jquery.signalR-2.1.1.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="http://localhost:9191/signalr/hubs"></script>
<script type="text/javascript">
        $(function () {
            $.connection.hub.url = "http://localhost:9191/signalr";
            var notif = $.connection.notificationsHub;

            notif.client.hello = function (msg)
            {
                alert(msg);
            }

            $.connection.hub.start().done(function () {
                $("#sayHello").click(function () {
                    notif.server.hello($("#myName").val());
                });
            });
        });
    </script>

The problem is this doesn't work in my correct setup... in the browser console I have no errors, the ~/signalr/hubs js looks fine... If I do more or less the same configuration, but host the SignalR in the ASP.NET MVC app, everything works as expected.

UPDATE: following @halter73 's suggestion to enable the client side logging for the hub, I've got the following error message which I still can't fix:

SignalR: notificationshub.Hello failed to execute. Error: Method not found: 'Microsoft.AspNet.SignalR.Hubs.IHubCallerConnectionContext Microsoft.AspNet.SignalR.Hub.get_Clients()'.

Could somebody please let me know what I am missing?

Thank you in advance!

Andrei

1
If you enable logging for the SignalR JS client, you might be able to get helpful diagnostics in you browser console. Just add $.connection.hub.logging = true; before your call to start().halter73
@halter73 Thank you for the suggestion! Did that and the error that shows up in the console when trying to call the method is: [09:10:30 GMT+0200] SignalR: notificationshub.Hello failed to execute. Error: Method not found: 'Microsoft.AspNet.SignalR.Hubs.IHubCallerConnectionContext Microsoft.AspNet.SignalR.Hub.get_Clients()'. Unfortunately, I can't find much info on this particular error so far. Any ideas anyone? Thanks!AndreiC
What version of SignalR are you using? Since 2.1.0, get_Clients should return a IHubCallerConnectionContext<dynamic> instead of IHubCallerConnectionContext. The error you are seeing could happen if you compile your application against SignalR <= 2.03 but loading SignalR >= 2.10 at runtime.halter73
@halter73 You just saved me! I would have never thought about it - especially that I installed SignalR.Core using Nuget and would have expected to have the same version (WEIRD that it didn't happen!). Thanks a lot for your help!AndreiC
@halter73 add an answer to my question so I could accept it :)AndreiC

1 Answers

1
votes

Since 2.1.0, get_Clients should return a IHubCallerConnectionContext<dynamic> instead of IHubCallerConnectionContext.

The error you are seeing could happen if you compile your application against SignalR <= 2.03 but loading SignalR >= 2.10 at runtime.