SignalR AspNetCore 无法连接到远程服务器---> System.Net.WebException

5

在我使用SignalR客户端应用程序连接SignalRHub时遇到了问题,下面是错误日志:

info: Microsoft.AspNetCore.Sockets.Client.WebSocketsTransport[0]
      02/01/2018 15:20:13: Connection Id f763a939-3fb9-4812-ae6e-dfe3198ab37b: Starting transport. Transfer mode: Text.
fail: Microsoft.AspNetCore.Sockets.Client.HttpConnection[9]02/01/2018 15:20:13: Connection Id f763a939-3fb9-4812-ae6e-dfe3198ab37b: Failed to start connection. Error starting transport 'WebSocketsTransport'.
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.WebException: The remote server returned an error: (404) Not Found.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__21.MoveNext()
   at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Sockets.Client.WebSocketsTransport.<Connect>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Sockets.Client.WebSocketsTransport.<StartAsync>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Sockets.Client.HttpConnection.<StartTransport>d__46.MoveNext()

SignalR中心正在无状态服务织物服务上运行,并部署在Azure服务织物集群上。

在服务器端,我使用nuget库Microsoft.AspNetCore.SignalR,以下是AspNetCore 2.0无状态服务端的参考代码:

Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace SampleChat
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddSignalR();
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
                app.UseDeveloperExceptionPage();

            app.UseMvc();

            app.UseFileServer();

            app.UseCors("CorsPolicy");

            app.UseWebSockets();

            app.UseSignalR(routes => { routes.MapHub<ChatHub>("SignalRHub"); });
        }
    }
}

ChatHub.cs:-

using Microsoft.AspNetCore.SignalR;

namespace SampleChat
{
    public class ChatHub : Hub
    {
        public void Send(string message)
        {
            // Call the broadcastMessage method to update clients.
            Clients.All.InvokeAsync("Send", message);
        }
    }
}

SampleChat.cs

using System.Collections.Generic;
using System.Fabric;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.ServiceFabric.Services.Communication.AspNetCore;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;

namespace SampleChat
{
    /// <summary>
    ///     The FabricRuntime creates an instance of this class for each service type instance.
    /// </summary>
    internal sealed class SampleChat : StatelessService
    {
        public SampleChat(StatelessServiceContext context)
            : base(context)
        {
        }

        /// <summary>
        ///     Optional override to create listeners (like tcp, http) for this service instance.
        /// </summary>
        /// <returns>The collection of listeners.</returns>
        protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            return new[]
            {
                new ServiceInstanceListener(serviceContext =>
                    new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                        return new WebHostBuilder()
                            .UseKestrel()
                            .ConfigureServices(
                                services => services
                                    .AddSingleton(serviceContext))
                            .UseContentRoot(Directory.GetCurrentDirectory())
                            .UseStartup<Startup>()
                            .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                            .UseUrls(url)
                            .Build();
                    }))
            };
        }
    }
}

在客户端,我正在使用Nuget库 Microsoft.AspNetCore.SignalR.Client

下面是SignalR客户端代码:

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleChatCoreApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SignalRConnector.ConnectoToSignalR().GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            };
            Console.ReadKey();
        }
    }
}

SignalRConnector.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;

namespace SampleChatCoreApplication
{
    static class SignalRConnector
    {
        public static async Task<bool> ConnectoToSignalR()
        {
            var connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:8634/SignalRHub")
                .WithConsoleLogger()
                .WithTransport(Microsoft.AspNetCore.Sockets.TransportType.WebSockets)
                .Build();
            connection.On<string>("Send", data =>
            {
                Console.WriteLine($"Received data: {data}");
            });

            await connection.StartAsync();

            await connection.InvokeAsync("Send", "Send data to Hub");

            Console.ReadKey();

            return true;
        }
    }
}

当我在本地系统上运行无状态服务时,我能够成功连接SignalR Hub。

但是当该服务在Azure Service Fabric集群上运行时,我不确定为什么连接SignalR客户端与SignalR Hub会遇到问题。

额外提供的信息是,我已经检查了网络和负载均衡器规则,并且从网络方面没有连通性问题(我通过在此服务中添加控制器进行验证,我能够从控制器中获取结果)。


你确定 http://localhost:8634/SignalRHub 是 SignalR 服务器代码的正确 URL 吗?错误提示显示该位置为 404。 - Muqeet Khan
由于安全原因,我无法提供我正在使用的实际 URL 来运行此应用程序。正如我在问题摘要中提到的,使用正确的 URL,我能够与使用相同 URL 托管的 MVC 控制器进行通信。因此,问题似乎与 AspNetCore SignalR 库本身有关。 - Narendra Jangir
你尝试过导航到YOURHUBURL/signalr/negotiate吗?如果hub已经正确初始化并启动,它应该会回答一个JSON文件。 - csharpwinphonexaml
1个回答

0
我进行了一些研究并发现问题可能与负载均衡器亲和性有关。为验证负载均衡器亲和性问题,我使用HTTP协议部署了应用程序,并能够在多次重试连接尝试中连接成功。这给了我一个提示,即负载均衡器亲和性可能会导致此处的问题。 进一步检查了应用程序端口的负载均衡器规则,并发现负载分配设置为“无”。根据我的理解,它应该是“SourceIPProtocol”。 目前,服务部署在一个5节点服务织物集群中,当我将服务缩减到1个节点时,我能够通过.net框架应用程序的HTTP和HTTPS协议在第一次尝试中连接到服务。
服务使用自签名证书部署。
这里唯一的问题是我无法从.net标准应用程序通过HTTPS协议连接到服务,并出现以下错误- 证书颁发机构无效或不正确 为了从.net框架应用程序连接,我在signalr连接代码之前编写了下面给出的代码- ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true; 上述代码在.net标准应用程序中无法工作。
通过进一步的研究,我发现在.NET Core中,ServicePointManager被替换为在HttpClientHandler实例本身上的配置。您需要在IHubConnectionBuilder实例上调用.WithMessageHandler并提供一个具有设置了ServerCertificateCustomValidationCallback的HttpClientHandler实例。
当我使用下面给出的代码时,我无法通过HTTPS协议连接到SignalR hub-
var handler = new HttpClientHandler
                        {
                            ClientCertificateOptions = ClientCertificateOption.Manual,
                            ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => { return true; }
                        };

                        Connection = new HubConnectionBuilder()
                            .WithUrl(url)
                            .WithTransport(Microsoft.AspNetCore.Sockets.TransportType.WebSockets)
                            .WithMessageHandler(handler)
                            .Build();

当我将传输方式更改为TransportType.ServerSentEvents或TransportType.LongPolling时,我能够与SignalR中心连接而没有任何问题。


另外,我发现在 Alpha2 中,“对于WebSockets,它不支持客户端证书身份验证。但是,在nightly Preview1构建中,我们可以使用HubConnectionBuilder上的.WithWebSocketOptions API来配置ClientWebSocketOptions对象,该对象上也有客户端证书配置。” - Narendra Jangir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接