【发布时间】: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