0

i'm following a guide for setting up signalr for my asp.net core project.

While following this guide, I have this snippet of code:

void SendMessage(string message)
{
   GlobalHost
  .ConnectionManager
  .GetHubContext<NotificationHub>().Clients.sendMessage(
message);
}

I have a NotificationHub file that looks like so:

public class NotificationHub : Hub
{
    public string Activate()
    {
        return "Monitor Activated";
    }
}

Globalhost is used to get a Hubcontext object. The issue is that When I import signalR, nothing called GlobalHost is available. In the documentation I can find this info about it:

GlobalHost

ASP.NET Core has dependency injection (DI) built into the framework. Services can use DI to 
access the HubContext. The GlobalHost object that is used in ASP.NET SignalR to get a HubContext 
doesn't exist in ASP.NET Core SignalR.

ok, so Globalhost is simply not available in core.

I need to do the same code, but for Microsoft.AspNetCore.SignalR;.

How can I get a hold of a Hubcontext object now?

EDIT

I have now tried following the very minimal example from the documentation, and create a small sample project. My ``startup.csfile has this line, inConfigureServices`:

services.AddSignalR();

and this line in Configure:

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<NotificationHub>("/Hubs");
    });

and my Hub file looks like this, as seen in the documentation:

namespace mvcCoreSample.Hubs
{
    public class NotificationHub : Hub
    {
        public Task SendMessage(string user, string message)
        {
            return Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }
}

Where Hubs, is a folder in the same directory as startup.cs. I have a controller that looks like this, which calls the hub:

public class msgController : Controller
{
    public IActionResult Index()
    {
        NotificationHub hub = new NotificationHub();
        hub.SendMessage("user1", "some message");
        return Content("serving content");
    }
}

But when I run this, and go to the url of the controller, the hub throws an error in the sendmessage function:

Microsoft.AspNetCore.SignalR.Hub.Clients.get returned null.

I really can't see where I am going wrong, something must be missing from the hub? perhaps something with setting up a connection?

EDIT 2

After a few corrections, I changed my controller to look like this:

public IActionResult Index(IHubContext<NotificationHub> hub)
{
    var clients = hub.Clients;
    return Content("serving content");
}

Even though I still did not know how to call my sendmessage function, I wanted to try this.

When running this site in debug mode, I get this error thrown in the browser:

InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.SignalR.IHubContext`1[[mvcCoreSample.Hubs.NotificationHub, mvcCoreSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'hub' parameter a non-null default value.
9
  • You dont have to create an instance of NotificationHub by yourself. Inject IHubContext<NotificationHub> in your controller's constructor. The thing is that when you create your own instance - it doesnt contain any users. Hubs are singleton and they persist the connections Commented Jun 20, 2021 at 21:13
  • great thx! the only final problem now is that if I switch it out now, so the controller looks like Index(IHubContext<NotificationHub> hub), then I can't write hub.sendmessage because it says that IHubContext<NotificationHub> does not contain a definition for sendmessage? Commented Jun 20, 2021 at 21:19
  • I now ran the example as is, and it fails Commented Jun 20, 2021 at 21:25
  • Use hub.Clients.All.SendAsyn(...) directly. Or, you can see how to use strongly typed hubs here: learn.microsoft.com/en-us/aspnet/core/signalr/… Commented Jun 20, 2021 at 21:32
  • Please, read the documentation properly! Commented Jun 20, 2021 at 21:33

1 Answer 1

2

You can inject it in a constructor of your service like this:

SomeService(IHubContext<NotificationHub> hub)

You are probably looking at some old documentation. Please, check the official docs here: Use hubs in SignalR for ASP.NET Core

Sign up to request clarification or add additional context in comments.

7 Comments

hmm, all right, the snippet I showed was supposed to just be placed somewhere in the homecontroller. Where Should i put the ``someservice` example?
Constructor of a class, that owns your SendMessage method. Probably HomeController in you case
ok, so just to be certain, my homecontrollers constructor should then take IHubContext as a an argument? The thing that is confusing me, is that I dont understand how that argument would be passed?
Framework will handle it for you via dependency injection. You'll have to configure it to work properly, check the Configure SignalR hubs section of the documentation I referred to
I've tried to go through the code in the documentation. It straight up does not work at all, and steps are missing. Do you know of any fully working minimal examples?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.