KyKo
KyKo

Reputation: 392

Connect to Azure SignalR service from a .NET Core console app?

I will start this off by saying that I am new to SignalR. I know the basic concepts, but have never personally written an app using SignalR until now.

With a little effort I have the basic Azure SignalR ChatRoom example running locally connected to an Azure SignalR instance (will only run using 'dotnet run' from the command line; if started from within Visual Studio it stops as soon as I try to change the name in the JS prompt window).

What I would like to do now is send messages (no need to receive) from a .NET Core console application and have them show up in the chatroom instance the browser is connected to. This is to simulate a background process doing some work and then sending a message to an Azure SignalR hub informing all clients of the outcome.

I created a new .NET Core app and when I try to add the Microsoft.Azure.SignalR package, I get an error message stating "error: Package 'Microsoft.Azure.SignalR' is incompatible with 'all' frameworks in project 'C:...\TestingAzureSignalR\ChatRoom.Console\ChatRoom.Console.csproj'."

Are there any examples of this anywhere that can help me out? Something as simple as working with the ChatRoom Quickstart example?

Upvotes: 1

Views: 2197

Answers (2)

Ken Chen
Ken Chen

Reputation: 141

On server you need to get a hub context using dependency injection and call SendAsync method to send to clients. For example:

public class Sender
{
    public Sender(IHubContext<MyHub> context)
    {
        context.Clients.All.SendAsync(...);
    }
}

One sample can be found here: https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/FlightMap, which uses a timer to broadcast data to clients.

For the "incompatible framework" error you met, you need to specify the version number when you add Microsoft.Azure.SignalR to your project as this package is still a prerelease:

dotnet add package Microsoft.Azure.SignalR --version 1.0.0-preview1-10011

Upvotes: 0

Lee Liu
Lee Liu

Reputation: 2091

If the version of .NET Core you used is under 2.1, please update it to 2.1 because SignalR was released on .NET Core 2.1.

More information about SignalR on .NET Core we can refer to: Get started with SignalR on ASP.NET Core

We can download .NET Core 2.1 SDK at: All Downloads of .NET Core

Similar thread for your reference:

SignalR cannot be used with .Net Core

Upvotes: 0

Related Questions