简体   繁体   English

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

[英]SignalR Error “No connection could be made because the target machine actively refused it”

I have a Single Page Application (SPA) with AngularJS front-end (client) and a Web API back-end (server). 我有一个带有AngularJS前端(客户端)和Web API后端(服务器)的单页应用程序(SPA)。 I also use SignalR for messaging between the client and the server. 我还使用SignalR在客户端和服务器之间进行消息传递。 Everything works fine on my development machine (with IIS Express), but when deployed to a server for testing, I get the following error when attempting to establish a connection to a hub: 在开发计算机(使用IIS Express)上,一切正常,但是当部署到服务器进行测试时,尝试建立与集线器的连接时出现以下错误:

Exception thrown: System.Net.Http.HttpRequestException: An error occurred while sending the request. 引发异常:System.Net.Http.HttpRequestException:发送请求时发生错误。 ---> 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 ---> 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()

Even though it connects successfully to the SignalR hub from the client: 即使它已从客户端成功连接到SignalR集线器:

SignalR集线器连接

The relevant part of my client side SignalR code is this (in an AnglarJS service): 我的客户端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'
    });

The error occurs in the following code snippet, which is in a Window Service attempting to establish a connection to the SignalR hub (the error line corresponds to 'CreateRequestService.cs:line 143' in the error message above): 错误发生在以下代码段中,该代码段在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);
        }

The SignalR hub lives in my Web API project: SignalR集线器位于我的Web API项目中:

SignalR集线器位置

And here are the project properties for the Web API project. 这是Web API项目的项目属性。 This is where the port number 60161 is set. 这是设置端口号60161的位置。 This number is different from the port on which my SPA runs because of cross domain compatibility: 由于跨域兼容性,此数字与SPA运行的端口不同:

APR项目属性

Any idea why this is happening? 知道为什么会这样吗?

So after much debugging, I finally found what the problem was. 因此,经过大量调试,我终于找到了问题所在。 There were, in fact, two different issues at play here. 实际上,这里有两个不同的问题。 First, since the code was deployed to a test server, "localhost" should not be used anywhere. 首先,由于代码已部署到测试服务器,因此“ localhost”不应在任何地方使用。 Instead, the URL of the test server with a suffix 'signalr' should be used, ie something like ' http://testserver.companyname.com/signalr '. 相反,应使用带有后缀“ signalr”的测试服务器的URL,即“ http://testserver.companyname.com/signalr ”之类的东西。 Second, in my client-side SignalR hub code I had the following line: 其次,在客户端的SignalR集线器代码中,我有以下内容:

transport: 'webSockets'

That was causing a problem because on our test server Web Sockets were not enabled and therefore there was no transport to use. 这引起了一个问题,因为在我们的测试服务器上未启用Web套接字,因此没有传输可用。 Once I commented out that line of code, it started working, but it was using Server Side Events (SSE) for transport, as Web Sockets were unavailable. 一旦我注释掉该行代码后,它便开始工作,但由于Web套接字不可用,它正在使用服务器端事件(SSE)进行传输。 After we went to IIS and installed and enabled them, it started using Web Sockets instead of SSE. 在进入IIS并安装并启用它们之后,它开始使用Web套接字而不是SSE。 So there you have it, the solution to that tricky problem. 这样就可以解决棘手的问题。 Hopefully this will help someone else so they don't have to spend the (about) 2 days I did to figure it out. 希望这会对其他人有所帮助,这样他们就不必花我大约2天的时间来弄清楚了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法建立连接,因为目标机器主动拒绝它 - Error: No connection could be made because the target machine actively refused it 收到错误消息“由于目标计算机主动拒绝连接而无法建立连接” - Geting Error “No connection could be made because the target machine actively refused it” 无法建立连接,因为目标计算机主动拒绝了它216.58.212.106:443 - No connection could be made because the target machine actively refused it 216.58.212.106:443 '由于目标机器主动拒绝它,因此无法建立连接 - 'No connection could be made because the target machine actively refused it 由于目标计算机主动拒绝连接,因此无法建立连接-客户端 - No connection could be made because the target machine actively refused it - client side 无法建立连接,因为目标计算机主动拒绝了127.0.0.1:8000 - No connection could be made because the target machine actively refused it 127.0.0.1:8000 由于目标计算机主动拒绝连接,因此无法建立连接-套接字程序 - No connection could be made because the target machine actively refused it - Socket Program 没有连接,因为目标机器主动拒绝它为什么? - No connection could be made because the target machine actively refused it why? “无法建立连接,因为目标机器主动拒绝它” Nethereum 异常 - “No connection could be made because the target machine actively refused it” Nethereum exception “无法建立连接,因为目标计算机主动拒绝了它” - “No connection could be made because the target machine actively refused it”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM