c# - Blazor 服务器 WebSocket 连接失败 : There was an error with the transport

标签 c# asp.net-core websocket blazor blazor-server-side

我在 blazor server 中有问题申请与 websockets .
在本地使用该应用程序我没有问题,但是当我将它加载到托管服务器上时,我从浏览器收到这些错误。
应用程序本身可以正常工作,但有时一些用户会收到消息“正在尝试重新连接到服务器”。
websocket error
这是我的 Startup.cs

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

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // ******
        // BLAZOR COOKIE Auth Code (begin)
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => options.ExpireTimeSpan = TimeSpan.FromDays(1));

        // BLAZOR COOKIE Auth Code (end)
        // ******

        services.AddMudServices();

        services.AddRazorPages();
        services.AddServerSideBlazor();
       
        services.AddHttpContextAccessor();


        // ******
        // BLAZOR COOKIE Auth Code (begin)
        // HttpContextAccessor
        services.AddHttpContextAccessor();
        services.AddScoped<HttpContextAccessor>();
        services.AddHttpClient();
        services.AddScoped<HttpClient>();
        // BLAZOR COOKIE Auth Code (end)
        // ******
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        // ******
        // BLAZOR COOKIE Auth Code (begin)
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        // BLAZOR COOKIE Auth Code (end)
        // ******

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapBlazorHub();
            endpoints.MapFallbackToPage("/_Host");
        });
    }
}
可能是什么问题呢 ?

最佳答案

我得到了同样的错误。我没有打开 Azure 应用服务的 ARR 关联。
打开它解决了这个问题。
看:
https://docs.microsoft.com/en-us/aspnet/core/signalr/publish-to-azure-web-app?view=aspnetcore-5.0#configure-the-app-in-azure-app-service

关于c# - Blazor 服务器 WebSocket 连接失败 : There was an error with the transport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67889297/

相关文章:

c# - 从另一个页面读取 IsolatedStorage 中的设置

C# 随机坐标是线性的

javascript - 在一个连接中将数据发送到不同的 websocket 端点

c# - 将 gRPC 服务绑定(bind)到 aspnetcore 中的特定端口

c# - Azure Blob 上传无法在 ASP .Net Core 应用程序中工作

python-3.x - 如何从 faust 应用程序向 Websocket 发送数据

javascript - WebSocket 网络错误 : OSStatus Error -9807: Invalid certificate chain

c# - 何时以及如何使用带有 MVP 和依赖项注入(inject)的 Windows 窗体创建演示者

c# - 为什么 WatiN 会卡住应用程序

c# - 扩展 BadResult 以提供不同的 StatusCode