【问题标题】:.NET Core SignalR client doesn't connect to ASP.NET Core hub.NET Core SignalR 客户端未连接到 ASP.NET Core 集线器
【发布时间】:2019-10-07 03:23:50
【问题描述】:

我有一个带有 SignalR Hub 的 ASP.NET Core Web API 和一个充当 Hub 客户端的 .NET Core 控制台应用程序。

控制台应用在尝试连接时抛出以下错误:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Date: Mon, 20 May 2019 18:12:44 GMT
  Server: Kestrel
  Content-Length: 0
})
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at SurveyApp.NotificationClient.Program.Main(String[] args) in C:\Users\user\source\repos\SurveyApp\SurveyApp.NotificationClient\Program.cs:line 16

Inner Exception 1:
HttpClientException: StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Date: Mon, 20 May 2019 18:12:44 GMT
  Server: Kestrel
  Content-Length: 0
}

我尝试了各种重命名路线和枢纽,但仍然不起作用。还尝试了此处显示的解决方案,因为它是一个类似的问题,但仍然没有结果:HttpClientException when connecting to working hub from SignalR .NET Client

服务器: DI注册:

            services.AddSignalR();

中间件注册:

            ...

            app.UseSignalR(route =>
            {
                route.MapHub<NotificationHub>("/notificationhub");
            });
            app.UseMvc();

集线器类:

public class NotificationHub : Hub
    {
        public Task SendMessage(string message)
        {
            return Clients.All.SendAsync(message);
        }

        public override Task OnConnectedAsync()
        {
            Console.WriteLine("A client connected");
            return base.OnConnectedAsync();
        }
    }

客户端代码:

static void Main(string[] args)
        {
            var connection = new HubConnection("https://localhost:5001/notificationhub");
            connection
                .CreateHubProxy("NotificationHub")
                .On<string>("ReceiveNotification", Console.WriteLine);
            connection.Start().Wait();
            Console.WriteLine("Connection started");
            Console.ReadKey();
        }

当我在connection.Start() 之后没有调用Wait() 时,我没有收到任何错误,但客户端仍然没有连接。我在服务器和客户端上都运行 .NET Core 2.2,SignalR nuget 的版本是 2.4.1

【问题讨论】:

    标签: asp.net-core signalr


    【解决方案1】:

    var connection = new HubConnection("https://localhost:5001/notificationhub");

    这种结构在 ASP.NET Core 中不再存在。似乎您正在将ASP.NET Core SignalR 服务器与Microsoft.AspNet.SignalR.Client 连接,它应该与ASP.NET SignalR 一起使用,但与ASP.NET Core 不兼容。

    如果是这种情况,您需要添加对Microsoft.AspNetCore.SignalR.Client 的引用,该引用以.NETStandard 2.0 为目标,并且也应该能够在.NET Framework 上运行。

    然后创建如下连接:

    var Connection = new HubConnectionBuilder()
        .WithUrl("https://localhost:5001/notificationhub")
        .ConfigureLogging(logging =>{
            logging.AddConsole();
        })
        .Build();
    

    更多详情请见https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2#connect-to-a-hub

    【讨论】:

    • 我不敢相信我浪费了半天时间玩错误的 nuget 包。谢谢!
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多