![](/img/trans.png)
[英]Error: 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)上,一切正常,但是当部署到服务器进行测试时,尝试建立与集线器的连接时出现以下错误:
引发异常:System.Net.Http.HttpRequestException:发送请求时发生错误。 ---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标计算机在System.Net上主动拒绝了127.0.0.1:60161 System.Net.ServicePoint.ConnectSocketInternal上的.Sockets.Socket.EndConnect(IAsyncResult asyncResult)(布尔值connectFailure,Socket s4,Socket s6,Socket&套接字,IPAddress&地址,ConnectSocketState状态,IAsyncResult asyncResult,Exception&exception)-内部异常的结尾堆栈跟踪--在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)在System.Net.HttpClientHandler.GetResponseCallback(IAsyncResult ar)-内部异常堆栈跟踪的结尾-在System.Runtime.CompilerServices。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的TaskAwaiter.ThrowForNonSuccess(任务任务),位于C:\\ Users ... \\ CreateRequestService.cs:第143行的CMR.Messaging.PublisherService.CreateRequestService.d__7.MoveNext()
即使它已从客户端成功连接到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 Service中试图建立与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套接字,因此没有传输可用。 一旦我注释掉该行代码后,它便开始工作,但由于Web套接字不可用,它正在使用服务器端事件(SSE)进行传输。 在进入IIS并安装并启用它们之后,它开始使用Web套接字而不是SSE。 这样就可以解决棘手的问题。 希望这会对其他人有所帮助,这样他们就不必花我大约2天的时间来弄清楚了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.