错误:无法完成与服务器的协商:错误:找不到 [英] Error: Failed to complete negotiation with the server: Error: Not Found

查看:706
本文介绍了错误:无法完成与服务器的协商:错误:找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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 再显示162帧 signalr.min.js:16 [2020-06-07T10:17:31.634Z]错误:无法启动连接:错误:未找到

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

这是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类代码:

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浏览器控制台的屏幕截图:

推荐答案

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

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

您应该还原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.UseSignalR(...);.

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

这篇关于错误:无法完成与服务器的协商:错误:找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆