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

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

我将 SignalR 与 ASP.NET Boilerplate .NET Core 3.1 一起使用,但我遇到了这个问题

错误:无法完成与服务器的协商:错误:未找到

如何在不跳过协商的情况下解决这个问题(提到的解决方案here

区域-evergreen.js:2845 POST http://localhost:21021/signalr/negotiate?enc_auth_token=wNYmO41%2F 显示 162 帧 signalr.min.js:16 [2020-06-07T10:17:31.634Z] 错误:无法启动连接:错误:未找到

这里是Angular代码:

  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
      });
  });
}

这是 .NET Core Class 代码:

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相关代码:

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?}");
            });
}

这是 Web 浏览器控制台屏幕截图:

该错误是为了 AbpCommonHub 连接到 /signalr,由 ABP 用于实时通知。
ABP 文档:https://aspnetboilerplate.com/Pages/Documents/Notification-System#real-time-notifications

您应该恢复 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?}");
});

顺便说一句,您可以删除 app.UseSignalR(...);,它已被弃用,取而代之的是 app.UseEndpoints(...);