locked
Host a SignalR Hub in a .NET Core 3.1 Console RRS feed

  • Question

  • User1373917027 posted

    Microsoft recommend to use Host.CreateDefaultBuilder instead of WebHost.CreateDefaultBuilder as I have understood. The problem is that I don't understand how to connect the call services.AddSignalR() to my Hub.

    In my old .NET 4.5 version it was easier from my point of view. The server was started with this code

    IDisposable oSignalRServer = WebApp.Start( "http://localhost:3211" );
    

    And the hub was referenced with

    ConnectionManager.GetHubContext<C_IOHub>()
    

    Hub definition

    [HubName( "IOHub" )]
    public class C_IOHub : Hub
    

    But with .NET Core I'm lost how to build this as a standalone server. All examples I have found describe how to attach the Hub to an existing MVC project. I have a Startup.cs with the following code:

    public static void Main( string [ ] args )
    {
        CreateHostBuilder( args ).Build().Run();
    }
    
    public static IHostBuilder CreateHostBuilder( string [ ] args ) =>
    Host.CreateDefaultBuilder( args )
    .ConfigureServices( ( hostContext, services ) =>
    {
        services.AddSignalR();
    } );
    

    I need the following information

    1. How do I create a standalone Hub in .NET Core?

    2. How do I obtain a reference to the Hub context?

    Thanks in advance!

    Friday, February 28, 2020 4:21 PM

All replies

  • User283571144 posted

    Hi ssstesoe,

    How do I create a standalone Hub in .NET Core?

    According to your description, I suggest you could try to follow below steps to use signlar in the asp.net core.

    1,Add below codes into the application.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    namespace CoreWebAPI
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
        }
    }
    

    2.Add the signalr hub library as this article shows and create the hub method.

    3.Add the Startup.cs file and add below codes:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using SignalRChat.Hubs;
    
    namespace SignalRChat
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
     
                services.AddSignalR();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
     
    app.UseRouting(); app.UseStaticFiles(); app.UseEndpoints(endpoints => { endpoints.MapHub<ChatHub>("/chatHub"); }); } } }

    How do I obtain a reference to the Hub context?

    Do you mean you want to know how to call the signlar hub in the server side?

    If this is your requirement, I suggest you could try to use asp.net core signalr client library. More details about how to use it, I suggest you could refer to this article.

    Best Regards,

    Brando

    Monday, March 2, 2020 1:50 AM