我们编写了一个 Blazer Server 应用程序 (.NET 7.0),托管在 Windows Server 2019 上的 IIS 10 上。该应用程序在最新版本的 Chrome 或 Edge 下运行良好。然而,我们的一些用户使用最新版本的 Firefox,他们偶尔无法加载该应用程序的页面。我找不到无法加载的原因,而且您使用该应用程序的时间越长,这种情况就越有可能发生。
在使用应用程序时保持开发者控制台打开,当网站无法加载时,我注意到以下情况:
Uncaught (in promise) WebSocket is not in the OPEN state
和 Uncaught (in promise) Error: cannot send data if the connection is not in the 'Connected' State.
我偶尔还会注意到,在 Chrome 的开发者控制台中,发送了一个
101 Switching Protocols
请求,其中包含 Upgrade: websocket
的 200 响应标头。我不知道这是否意味着 SignalR 在某种程度上失败了,并且“原始”WebSockets 被用作 Chrome 中的后备,也许表明 Firefox 尝试后备失败了?
我已通过 Firefox 中的
about:config
确认 network.http.http2.websockets
设置为 true。我还重新启动了 IIS Web 服务器,以防万一导致问题,并确认 WebSockets 已作为 IIS 的一部分安装。
除此之外,我对 SignalR 和 WebSockets 非常不熟悉(这是我们的第一个 Blazor 服务器应用程序),并且我对 Stack Overflow 的谷歌搜索和评论没有发现任何相关内容。
我的基本理解是,我们通常希望使用 SignalR,它恰好在幕后使用 WebSocket,但我们不想使用“原始 WebSocket”(正如 Microsoft 在本页中提到的那样)。我还认为 Blazor Server 使用 SignalR 而不需要额外的配置,但也许我们一直配置错误,只是依赖于后备。
如果有用,这是我们的
Program.cs
:
using Microsoft.EntityFrameworkCore;
using miniDARTS.Data;
using MudBlazor.Services;
using Microsoft.AspNetCore.Authentication.Negotiate;
using miniDARTS.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// Blazor Server doesn't include HttpClient service by default.
builder.Services.AddHttpClient();
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
// Configure the database connection.
string connectionString = "";
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
{
connectionString = builder.Configuration.GetConnectionString("miniDARTSContext");
}
else if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
{
connectionString = builder.Configuration.GetConnectionString("testing");
}
var serverVersion = new MySqlServerVersion(new Version(8, 0, 28));
builder.Services.AddDbContextFactory<miniDARTSContext>(options => options.UseMySql(connectionString, serverVersion), ServiceLifetime.Scoped);
// Only run cron jobs if they are in a production environment.
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production")
{
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<ScopedBackgroundService>();
services.AddScoped<IScopedProcessingService, PhaseChangeReportService>();
})
.Build();
host.RunAsync();
}
// Add MudBlazor services.
builder.Services.AddMudServices();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
非常具体的问题,可能是 Firefox 错误,但我能想到可以检查以下几件事:
enabled
设置为 true
? ...
<script src="_framework/blazor.server.js" autostart="true"></script>
<script>
Blazor.start({
configureSignalR: function (builder) {
builder.withUrl('/_blazor', { transport: signalR.HttpTransportType.WebSockets | signalR.HttpTransportType.LongPolling });
}
});
</script>
</body>
尝试显式添加 signalR 和 websockets:
builder.Services.AddSignalR();
app.UseWebSockets();
在 Program.cs 中
确保 Blazor 和相关软件包是解决方案中的最新版本。
尝试在 Firefox 上运行它并禁用所有扩展?
请检查我在另一个问题中的回答:Blazor 导航无法在 Firefox 上运行,为什么?,这解释了这里发生的情况