SignalR 错误“无法建立连接,因为目标机器主动拒绝它”

luk*_*egf 3 c# asp.net signalr asp.net-web-api angularjs

我有一个带有 AngularJS 前端(客户端)和一个 Web API 后端(服务器)的单页应用程序 (SPA)。我还使用 SignalR 在客户端和服务器之间进行消息传递。在我的开发机器上一切正常(使用 IIS Express),但是当部署到服务器进行测试时,我在尝试与集线器建立连接时收到以下错误:

抛出异常:System.Net.Http.HttpRequestException:发送请求时出错。---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 无法建立连接,因为目标机器在 System.Net 127.0.0.1:60161 主动拒绝它.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) ---内部异常结束堆栈跟踪 --- 在 System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.Http.HttpClientHandler。

即使它从客户端成功连接到 SignalR 集线器:

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'
    });
Run Code Online (Sandbox Code Playgroud)

错误发生在以下代码片段中,该代码片段位于尝试与 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);
        }
Run Code Online (Sandbox Code Playgroud)

SignalR 中心位于我的 Web API 项目中:

SignalR 集线器位置

以下是 Web API 项目的项目属性。这是设置端口号 60161 的地方。由于跨域兼容性,此数字与运行我的 SPA 的端口不同:

APR 项目属性

知道为什么会这样吗?

luk*_*egf 5

所以经过多次调试,我终于找到了问题所在。事实上,这里有两个不同的问题在起作用。首先,由于代码已部署到测试服务器,因此不应在任何地方使用“localhost”。相反,应该使用带有后缀“signalr”的测试服务器的 URL,即类似于“ http://testserver.companyname.com/signalr ”。其次,在我的客户端 SignalR 集线器代码中,我有以下行:

transport: 'webSockets'
Run Code Online (Sandbox Code Playgroud)

这导致了一个问题,因为在我们的测试服务器上没有启用 Web 套接字,因此没有可以使用的传输。一旦我注释掉那行代码,它就开始工作,但它使用服务器端事件 (SSE) 进行传输,因为 Web 套接字不可用。在我们访问 IIS 并安装并启用它们之后,它开始使用 Web Sockets 而不是 SSE。所以你有了它,这个棘手问题的解决方案。希望这会对其他人有所帮助,因此他们不必花费我所做的(大约)2 天来弄清楚。