SignalR possibly not working in .NET 5? Unable to resolve service for type 'Microsoft.AspNetCore.SignalR.IHubContext`1[iBet.Server.Hubs.ChatHub]'

Viewed 732

I'm trying to setup a chat in a .NET 5 and Angular 10 application but so far the backend part has put me to a hold. Whenever I try to access the ChatController I get

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.SignalR.IHubContext`1[iBet.Server.Hubs.ChatHub]' while attempting to activate 'iBet.Server.Controllers.ChatController'.

This is the controller itself:

    [Produces("application/json")]
public class ChatController : ApiController
{
    private readonly IHubContext<ChatHub> _hubContext;

    public ChatController(IHubContext<ChatHub> hubContext)
    {
        _hubContext = hubContext;
    }

    [Route("send")]                                           
    [HttpPost]
    public IActionResult SendRequest([FromBody] MessageRequest message)
    {
        _hubContext.Clients.All.SendAsync("ReceiveOne", message.User, message.MessageText);
        return Ok(message.MessageText);
    }
}

And this is my startup:

        public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        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("/Home/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.UseCors(options => options
           .AllowAnyOrigin()
           .AllowAnyHeader()
           .AllowAnyMethod());

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<ChatHub>("/chatsocket");    
        });

    }

From what I understand once you put services.AddSignalR(); it takes care of the DI but it seems it's different in .NET 5, any suggestions would be greatly appreciated.

0 Answers
Related