SignalR 错误 "No connection could be made because the target machine actively refused it"
SignalR Error "No connection could be made because the target machine actively refused it"
我有一个带有 AngularJS 前端(客户端)和一个 Web API 后端(服务器)的单页应用程序 (SPA)。我还使用 SignalR 在客户端和服务器之间进行消息传递。在我的开发机器(使用 IIS Express)上一切正常,但是当部署到服务器进行测试时,在尝试建立与集线器的连接时出现以下错误:
Exception thrown: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:60161
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at CMR.Messaging.PublisherService.CreateRequestService.d__7.MoveNext() in C:\Users...\CreateRequestService.cs:line 143
即使它从客户端成功连接到 SignalR 集线器:
我的客户端 SignalR 代码的相关部分是这样的(在 AnglarJS 服务中):
// Setting up the SignalR hub
var hub = new Hub("reportHub", {
listeners: {
'newConnection': function (id) {
},
'broadcastReportProgress': function (reportProgress) {
Reports.add(reportProgress[0].clientName, reportProgress[0].folderPath, reportProgress[0].reportName, reportProgress[0].status, reportProgress[0].userName);
$rootScope.$apply();
},
'broadcastUserGenerating': function(reportProgress) {
Reports.addActiveUser(reportProgress.clientName, reportProgress.status, reportProgress.userName);
$rootScope.$apply();
}
},
methods: ['send'],
errorHandler: function(error) {
console.error(error);
},
hubDisconnected: function () {
if (hub.connection.lastError) {
hub.connection.start();
}
},
transport: 'webSockets',
logging: true,
queryParams: { userName: authService.authentication.userName },
rootPath: 'http://localhost:60161/signalr'
});
错误发生在以下代码片段中,该代码片段位于 Window 试图建立与 SignalR 集线器的连接的服务中(错误行对应于上面错误消息中的 'CreateRequestService.cs:line 143') :
try
{
IList<ReportProgress> generationProgress = new List<ReportProgress>();
generationProgress.Add(new ReportProgress
{
ClientName = clientName,
FolderPath = response.FolderPath,
ReportName = response.Response,
Status = "InProgress",
UserName = response.UserName
});
var hubConnection = new HubConnection("http://localhost:60161/signalr", false);
IHubProxy reportHubProxy = hubConnection.CreateHubProxy("ReportHub");
**await hubConnection.Start(); --> ERROR OCCURS HERE**
await reportHubProxy.Invoke("SendReportProgress", generationProgress);
await reportHubProxy.Invoke("SendUserGenerating", generationProgress[0]);
}
catch (Exception ex)
{
throw new Exception(nameof(this.GetType) + " ****Exception**** ", ex);
}
SignalR 中心位于我的 Web API 项目中:
这里是 Web API 项目的项目属性。这是设置端口号 60161 的地方。由于跨域兼容性,此数字与我的 SPA 运行的端口不同:
知道为什么会这样吗?
所以经过多次调试,我终于找到了问题所在。事实上,这里有两个不同的问题在起作用。首先,由于代码已部署到测试服务器,因此不应在任何地方使用 "localhost"。相反,应该使用带有后缀 'signalr' 的测试服务器的 URL,即类似“http://testserver.companyname.com/signalr”的东西。其次,在我的客户端 SignalR 中心代码中,我有以下行:
transport: 'webSockets'
这导致了问题,因为在我们的测试服务器上未启用 Web 套接字,因此没有传输可供使用。一旦我注释掉那行代码,它就开始工作了,但它使用服务器端事件 (SSE) 进行传输,因为 Web 套接字不可用。在我们转到 IIS 并安装并启用它们之后,它开始使用 Web Sockets 而不是 SSE。
所以你有它,这个棘手问题的解决方案。希望这会对其他人有所帮助,这样他们就不必花费(大约)2 天的时间来解决这个问题。
我有一个带有 AngularJS 前端(客户端)和一个 Web API 后端(服务器)的单页应用程序 (SPA)。我还使用 SignalR 在客户端和服务器之间进行消息传递。在我的开发机器(使用 IIS Express)上一切正常,但是当部署到服务器进行测试时,在尝试建立与集线器的连接时出现以下错误:
Exception thrown: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:60161 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) --- End of inner exception stack trace --- at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at CMR.Messaging.PublisherService.CreateRequestService.d__7.MoveNext() in C:\Users...\CreateRequestService.cs:line 143
即使它从客户端成功连接到 SignalR 集线器:
我的客户端 SignalR 代码的相关部分是这样的(在 AnglarJS 服务中):
// Setting up the SignalR hub
var hub = new Hub("reportHub", {
listeners: {
'newConnection': function (id) {
},
'broadcastReportProgress': function (reportProgress) {
Reports.add(reportProgress[0].clientName, reportProgress[0].folderPath, reportProgress[0].reportName, reportProgress[0].status, reportProgress[0].userName);
$rootScope.$apply();
},
'broadcastUserGenerating': function(reportProgress) {
Reports.addActiveUser(reportProgress.clientName, reportProgress.status, reportProgress.userName);
$rootScope.$apply();
}
},
methods: ['send'],
errorHandler: function(error) {
console.error(error);
},
hubDisconnected: function () {
if (hub.connection.lastError) {
hub.connection.start();
}
},
transport: 'webSockets',
logging: true,
queryParams: { userName: authService.authentication.userName },
rootPath: 'http://localhost:60161/signalr'
});
错误发生在以下代码片段中,该代码片段位于 Window 试图建立与 SignalR 集线器的连接的服务中(错误行对应于上面错误消息中的 'CreateRequestService.cs:line 143') :
try
{
IList<ReportProgress> generationProgress = new List<ReportProgress>();
generationProgress.Add(new ReportProgress
{
ClientName = clientName,
FolderPath = response.FolderPath,
ReportName = response.Response,
Status = "InProgress",
UserName = response.UserName
});
var hubConnection = new HubConnection("http://localhost:60161/signalr", false);
IHubProxy reportHubProxy = hubConnection.CreateHubProxy("ReportHub");
**await hubConnection.Start(); --> ERROR OCCURS HERE**
await reportHubProxy.Invoke("SendReportProgress", generationProgress);
await reportHubProxy.Invoke("SendUserGenerating", generationProgress[0]);
}
catch (Exception ex)
{
throw new Exception(nameof(this.GetType) + " ****Exception**** ", ex);
}
SignalR 中心位于我的 Web API 项目中:
这里是 Web API 项目的项目属性。这是设置端口号 60161 的地方。由于跨域兼容性,此数字与我的 SPA 运行的端口不同:
知道为什么会这样吗?
所以经过多次调试,我终于找到了问题所在。事实上,这里有两个不同的问题在起作用。首先,由于代码已部署到测试服务器,因此不应在任何地方使用 "localhost"。相反,应该使用带有后缀 'signalr' 的测试服务器的 URL,即类似“http://testserver.companyname.com/signalr”的东西。其次,在我的客户端 SignalR 中心代码中,我有以下行:
transport: 'webSockets'
这导致了问题,因为在我们的测试服务器上未启用 Web 套接字,因此没有传输可供使用。一旦我注释掉那行代码,它就开始工作了,但它使用服务器端事件 (SSE) 进行传输,因为 Web 套接字不可用。在我们转到 IIS 并安装并启用它们之后,它开始使用 Web Sockets 而不是 SSE。 所以你有它,这个棘手问题的解决方案。希望这会对其他人有所帮助,这样他们就不必花费(大约)2 天的时间来解决这个问题。