I am currently trying out the RC2
release of ASP.NET Core
and I am running into an issue with SignalR
. I need to be able to send messages to the client outside of the request thread.
Now in the full .NET framework you can do this like:
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.<SendMessage>()
But in ASP.NET
Core there is no GlobalHost
.
I found a similar question: How to get SignalR Hub Context in a vNext Project?
Where the second answer provides a method to get the hubcontext outside the request thread, but this also does not work in ASP.NET Core
.
So my question is: how can I get the hub context outside of the request scope in ASP.NET Core?
Another possibility is to inject your HubContext into your controller like:
Then you can also call
But then you will direct call methods on all clients.
See also Call SignalR Core Hub method from Controller for an other possibility
You will have to pull the current github version from : Signalr Github (Commit: b95ac7b at time of writing)
Once you have this, and have loaded the solution, or added all three of the projects to your existing solution, you will need to change project.json in all three projects.
Microsoft.AspNetCore.SignalR.Server - project.json
You will see references to version 1.1.0-* (RC3) of each assembly. Change these to the current RC2, until you see the following
Now save the file, and the dependencies will update.
Do the same with The Messaging and Infrastructure project.json file's, replacing any 1.1.0-* with 1.0.0
Once that is done, you can add a project reference to your main project of Microsoft.AspNetCore.SignalR.Server
Now that you have that loaded, Open your Startup.cs
Inside the ConfigureServices method, add:
Inside the Configure Method add:
Next, add a using statement and import the Infrastructure namespace as follows:
And finally Create a static property in Startup.cs called ConnectionManager as follows:
Finally add a IServiceProvider property to the Configure method in Startup.cs (Need to import the System namespace). Then Load the ConfigurationManager from this.
Now, in your hubs / anywhere else, instead of using Globalhost, Simply use startup. For example: