Using project "Asp.Net Core Web API" with SignalR using .Net 7.0 or higher, how to add a route to a signalR Hub?

1.8k Views Asked by At

I try add signalR to a new ASP.NET Core Web API project created for .net 7.0.

I try to follow the sample found at: GitHub AspNetCore.Docs/aspnetcore/signalr/ Without success because they pass through a StartUp class which seems to be the old way to do the job.

Also, the sample use IApplicationBuilder instead of a WebApplication object as the template create for us for a new project in Visual Studio 2022.

So how to define a route for our hub?

1

There are 1 best solutions below

0
Eric Ouellet On

See doc: Use hubs in SignalR for ASP.NET Core

Use:

MapHub<youHub>("route");

Just for reference:

public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            //EO start:
            builder.Services.AddSignalR();
            builder.Services.AddDbContext<MicroReseauDbContext>();
            //EO end.

            AppGlobal.Instance.ShowErrorAction = (msg)=>throw new HttpRequestException($"Error occurs: {msg}");
            AppGlobal.Instance.ShowWarningAction = (msg) => throw new HttpRequestException($"Warning occurs: {msg}");

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();

            app.MapControllers();

            //EO start:
            app.MapHub<LogHub>("Log/LogHub");
            //EO end.

            app.Run();
        }
    }