Search code examples
.netazureasp.net-coresignalr

Project Architecture For Hosting SignalR Hub On Azure Without Using Web


I want to host simple signalR app on azure to send chat messages for my mobile/web chat application. I am lost in the jungle of that is microsofts documentation with 99% depricated examples.

Now i got the following questions:

  1. Do i need to register "Microsoft.Web" for my subscribtion or is there a cleaner way around this?
  2. What is the ASP.NET Core project template with the least overhead for my simple signalR hub (I dont need any web pages)

In my working project i used "ASP.NET Core Web App" as a starting point and hooked up a simple hub. But when i publish the app i was asked to register "Microsoft.Web" for my subscription.

! * The subscription is not registered to use namespace 'Microsoft.Web'. ! *`


Solution

  • About the error message you got, I think this answer already had a great explanation. Maybe we can create an Azure web app manually in Azure portal then choose this resource when publishing app in VS.

    And there's no template right now in visual studio to create asp.net core signalr project. But we only need to create a Hub and register it into a web app for your requirement following this document. Creating a Hub like below:

    using Microsoft.AspNetCore.SignalR;
    
    namespace SignalRChat.Hubs
    {
        public class ChatHub : Hub
        {
            public async Task SendMessage(string user, string message)
            {
                await Clients.All.SendAsync("ReceiveMessage", user, message);
            }
        }
    }
    

    Then use it in Program.cs, if you had standalone client application as the signalr client, you may need to add Cors policy.

    builder.Services.AddSignalR();
    ...
    app.MapHub<ChatHub>("/chatHub");
    

    If you need to use Azure Signalr Service, you can have below code instead.

    builder.Services.AddSignalR()
        .AddAzureSignalR(options => {
            options.ConnectionString = connection_string";
        });