typescriptsignalraspnetboilerplate.net-core-3.1asp.net-boilerplate

Error: Failed to complete negotiation with the server: Error: Not Found


I'm using SignalR with ASP.NET Boilerplate .NET Core 3.1 but I encounterd this problem

Error: Failed to complete negotiation with the server: Error: Not Found

How could I resolve this problem without skip negotiate (solution mentioned here )

zone-evergreen.js:2845 POST http://localhost:21021/signalr/negotiate?enc_auth_token=wNYmO41%2F Show 162 more frames signalr.min.js:16 [2020-06-07T10:17:31.634Z] Error: Failed to start the connection: Error: Not Found

here is Angular Code:

  ngOnInit(): void {
    this.renderer.addClass(document.body, 'sidebar-mini');

    //SignalRAspNetCoreHelper.initSignalR();
     // SignalRAspNetCoreHelper.initSignalR(); // Replace this line with the block below
     SignalRAspNetCoreHelper.initSignalR(() => {
      var chatHub = null;

      abp.signalr.startConnection(abp.appPath + 'signalr-myChatHub', function (connection) {
          chatHub = connection; // Save a reference to the hub


          connection.on('getMessage', function (message) { // Register for incoming messages
              console.log('received message: ' + message);
          });
      }).then(function (connection) {
          abp.log.debug('Connected to myChatHub server!');
          abp.event.trigger('myChatHub.connected');
      });

      abp.event.on('myChatHub.connected', function() { // Register for connect event
          chatHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
      });
  });
}

and here is .NET Core Class code :

using Abp.Dependency;
using Abp.Runtime.Session;
using Castle.Core.Logging;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace HealthMapControlPanel.ChatAppService
{
   public class MyChatHub : Hub, ITransientDependency
    {
        public IAbpSession AbpSession { get; set; }

        public ILogger Logger { get; set; }

        public MyChatHub()
        {
            AbpSession = NullAbpSession.Instance;
            Logger = NullLogger.Instance;
        }

        public async Task SendMessage(string message)
        {
            await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
        }

        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            await base.OnDisconnectedAsync(exception);
            Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
        }
    }
}

Startup.cs class-related Code :

public void Configure(IApplicationBuilder app,  ILoggerFactory loggerFactory)
{
  app.UseSignalR(routes =>
            {
                routes.MapHub<MyChatHub>("/signalr-myChatHub");
            });

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<MyChatHub>("/signalr-myChatHub");
                endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
            });
}

and here is Web Browser Console Screenshot:

enter image description here


Solution

  • That error is for connecting to /signalr for AbpCommonHub, used by ABP for real-time notifications.
    ABP document: https://aspnetboilerplate.com/Pages/Documents/Notification-System#real-time-notifications

    You should restore endpoints.MapHub<AbpCommonHub>("/signalr");.

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<AbpCommonHub>("/signalr"); // Restore this
        endpoints.MapHub<MyChatHub>("/signalr-myChatHub");
        endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapControllerRoute("defaultWithArea", "{area}/{controller=Home}/{action=Index}/{id?}");
    });
    

    By the way, you can remove app.UseSignalR(...);, which has been deprecated in favour of app.UseEndpoints(...);.