typescript - 错误 : Failed to complete negotiation with the server: Error: Not Found

标签 typescript signalr aspnetboilerplate .net-core-3.1 asp.net-boilerplate

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

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

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

zone-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] 错误:无法启动连接:错误:未找到

这是角度代码:

  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 类代码:
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 类相关代码:
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 浏览器控制台屏幕截图:

enter image description here

最佳答案

该错误用于连接到 /signalrAbpCommonHub ,由 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(...); .

关于typescript - 错误 : Failed to complete negotiation with the server: Error: Not Found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62243988/

相关文章:

Asp.net Boilerplate - 没有给出与所需的形式参数相对应的参数

Javascript 过滤数组中的元素,该数组将另一个数组中的元素保存在一起

javascript - 如何在 Typescript 和 Angular 2 中使用 "@input set"检索数据?

javascript - 如何在不打开打印机设置页面的情况下在 ionic 应用程序中使用 print.js 进行打印

c# - 管理匿名用户的 SignalR 连接

signalr - 缩小信号中心自动生成的代理

powershell - 从NuGet安装SignalR(Newtonsoft.Json的问题)

c# - EF Core - 在一个请求中添加/更新实体和添加/更新/删除子实体

typescript - "Structural type guard"与 `if` 一起使用,但不能作为数组过滤谓词

c# - EventBus 未使用 Abp 触发 ASP.NET Core 上的事件