我有一个运行预览SignalR的.NET Core 2.0应用程序。我在IIS 8.5后面的Windows Server 2012 R2上运行。
Websockets在服务器角色中启用,也在IIS中配置。
当我尝试通过Websocket启动SignalR连接时,我得到了这个回复:
WebSocket握手期间出错:意外的响应代码:204
我读过的所有内容都说这应该可行,据我所知,我已正确设置了所有配置。任何人都可以指导我调试这个问题吗?
更详细信息-------
我的应用程序有两种扩展方法,用于将SignalR添加到应用程序和服务中:
/// <summary>
/// .NET Core 2.0-specific extensions for Signal-R.
/// </summary>
public static class SignalRExtensions
{
/// <summary>
/// Use Signal-R and set up the base hub in the application.
/// </summary>
/// <param name="app">The Builder for the .NET Core 2.0 application.</param>
/// <param name="serviceProvider">The application's service provider.</param>
/// <returns>A reference to the application builder object.</returns>
public static IApplicationBuilder UseSignalR_NETCORE20(this IApplicationBuilder app, IServiceProvider serviceProvider)
{
app.UseSignalR(routes =>
{
routes.MapHub<BaseHub>("baseHub");
});
SignalRHelper.BaseHubContext = serviceProvider.GetService<IHubContext<BaseHub>>();
return app;
}
/// <summary>
/// Add Signal-R servies to a .NET Core 20 application.
/// </summary>
/// <param name="services">The service collection.</param>
/// <returns>The Signal-R builder object.</returns>
public static ISignalRBuilder AddSignalR_NETCORE20(this IServiceCollection services)
{
var settings = new JsonSerializerSettings();
settings.ContractResolver = new SignalRContractResolver();
var serializer = JsonSerializer.Create(settings);
services.Add(new ServiceDescriptor(typeof(JsonSerializer), provider => serializer, ServiceLifetime.Transient));
services.AddSingleton<IMessageSender, MessageSender>();
services.AddSingleton<IContractResolver, SignalRContractResolver>();
return services.AddSignalR();
}
}
如果我们在Windows Server 2012 R2或更高版本上运行,它有代码可以使用Web套接字(我在.NET Core 1.1中需要这个代码,我可能不需要在2.0中检查但是还没有通过验证还):
// Don't enable web sockets if we're running on a Windows version lower than 6.2 (Windows 8/Windows Server 2012 R2)
Regex windowsRex = new Regex(@"Microsoft Windows ([0-9]*\.[0-9*]).*");
Match match = windowsRex.Match(RuntimeInformation.OSDescription);
if (!match.Success || double.Parse(match.Groups[1].Value) > 6.1)
{
app.UseWebSockets();
}
客户端连接代码看起来像这样(稍微清理一下):
Publisher.signalRHubURL = /* set URL */
Publisher.options = { logging: false };
Publisher.signalRHub = new signalR.HubConnection(Publisher.signalRHubURL, Publisher.options);
Publisher.showConnectionStatus('disconnected', 'Disconnected. Real-Time updates offline.');
Publisher.signalRHub.start().then(function() {
Publisher.isInitialized = true;
Publisher.signalRHub.on("updateSubscribedDisplay", function(jsonData) {
Publisher.startMessageQueueThread(jsonData);
});
Publisher.signalRHub.onclose(Publisher.reconnect);
if (Publisher.subscriberQueue.length > 0) {
/* send some data to the server */
}
Publisher.showConnectionStatus('connected', 'Data connection is normal.');
}).catch(function(error) {
// WebSockets not supported, fall back to LongPolling
Publisher.logError(error, 1);
Publisher.options.transport = signalR.TransportType.LongPolling;
Publisher.signalRHub = new signalR.HubConnection(Publisher.signalRHubURL, Publisher.options);
Publisher.signalRHub.start().then(function() {
Publisher.isInitialized = true;
Publisher.signalRHub.on("updateSubscribedDisplay", function(jsonData) {
Publisher.startMessageQueueThread(jsonData);
});
Publisher.signalRHub.onclose(Publisher.reconnect);
if (Publisher.subscriberQueue.length > 0) {
/* send some data to the server */
}
Publisher.showConnectionStatus('connected', 'Data connection is normal.');
});
});
我正在使用完整的IIS,并启用了webSockets:
在网络选项卡中,我看到webSocket请求失败,然后longPolling请求接管:
我注意到一旦建立连接,signalR就会将WebSockets报告为可用传输:
更详细信息-------
我在Startup类的配置方法的最顶部添加了这个:
app.Use(async (context, next) =>
{
ServerFactory.GetLogManager().Log(string.Format("Request Scheme: {0}", context.Request.Scheme));
await next();
});
这总是记录&#34; http&#34;作为计划(我期望至少有一个&#34; ws&#34;对于webSocket请求)。这是一个有效的测试吗?是否表明IIS在将其传递给Kestrel之前以某种方式破坏了webSocket请求?
可能与ANCM问题163(https://github.com/aspnet/AspNetCoreModule/issues/163)有关?
我在web.config中有这个重写规则: