Nei*_*man 7 javascript firefox websocket signalr blazor-server-side
我们编写了一个 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发送了一个请求,其中包含 200 响应标头Upgrade: websocket。我不知道这是否意味着 SignalR 在某种程度上失败了,并且“原始”WebSockets 被用作 Chrome 中的后备,也许表明 Firefox 的后备尝试失败了?
我已经通过about:configFirefox确认network.http.http2.websockets设置为 true。我还重新启动了 IIS Web 服务器,以防万一导致问题,并确认 WebSockets 已作为 IIS 的一部分安装。
除此之外,我对 SignalR 和 WebSockets 非常不熟悉(这是我们的第一个 Blazor 服务器应用程序),并且我的 Google 搜索和 Stack Overflow 审查没有发现任何相关内容。
我的基本理解是,我们通常希望使用 SignalR,它恰好在幕后使用 WebSockets,但我们不想使用“原始 WebSockets”(正如 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();
Run Code Online (Sandbox Code Playgroud)
非常具体的问题,可能是 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>
Run Code Online (Sandbox Code Playgroud)
尝试显式添加 signalR 和 websockets:
builder.Services.AddSignalR();
Run Code Online (Sandbox Code Playgroud)
app.UseWebSockets();
Run Code Online (Sandbox Code Playgroud)
在程序.cs中
确保 Blazor 和相关包是解决方案中的最新版本。
尝试在 Firefox 上运行它并禁用所有扩展?
| 归档时间: |
|
| 查看次数: |
1370 次 |
| 最近记录: |