Nar*_*gir 5 stateless websocket signalr azure-service-fabric asp.net-core-mvc-2.0
我从 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()
Run Code Online (Sandbox Code Playgroud)
SignalR hub 与无状态服务结构服务一起运行,并部署在 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"); });
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例聊天.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();
}))
};
}
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端,我使用 nuget 库Microsoft.AspNetCore.SignalR.Client
下面给出的是 SignalR 客户端代码-
程序.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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我在本地系统中运行无状态服务时,我能够成功连接 SignalR Hub。
当服务在 azure 服务结构集群上运行时,我不确定为什么在将 SignalR 客户端与 SignalR 集线器连接时遇到问题。
对于附加信息,我检查了网络和负载均衡器规则,并且网络端不存在连接问题。(我已经通过在此服务中添加控制器来验证它,并且我能够从控制器检索结果)。
我做了一些研发,发现该问题可能与负载均衡器亲和力有关。为了验证负载均衡器关联性问题,我已经使用 HTTP 协议部署了应用程序,并且能够在多次重试连接尝试中与其连接。这给了我一个提示,负载均衡器亲和性可能会导致这里的问题。此外,我还检查了应用程序端口的负载均衡器规则,发现负载分配设置为无。根据我的理解,它应该是“SourceIPProtocol”。目前,该服务部署在 5 节点服务结构集群中,当我将服务缩减到 1 个节点时,我能够使用 .net 框架应用程序通过 HTTP 和 HTTPS 协议在第一次尝试中连接到该服务。
使用自签名证书部署服务。
这里剩下的唯一问题是我无法通过 HTTPS 协议从 .net 标准应用程序连接服务并收到错误 -
The certificate authority is invalid or incorrect
为了从 .net 框架应用程序进行连接,我在信号器连接代码之前在给定行下方编写 -
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
上面给出的代码在 .net 标准应用程序中不起作用。
通过进一步的研发,我发现在.NET Core中,ServicePointManager被HttpClientHandler实例本身的配置所取代。您需要调用.WithMessageHandlerIHubConnectionBuilder 实例并提供带有 set 的 HttpClientHandler 实例ServerCertificateCustomValidationCallback。
当我使用下面给定的代码时,我无法通过 HTTPS 协议连接 SignalR 集线器 -
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();
Run Code Online (Sandbox Code Playgroud)
当我将传输更改为 TransportType.ServerSentEvents 或 TransportType.LongPolling 时,我能够毫无问题地连接 SignalR 集线器。
| 归档时间: |
|
| 查看次数: |
4391 次 |
| 最近记录: |