You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure部署Blazor Server应用因大量_blazor请求致不稳定问题排查

问题:Blazor Server部署Azure App Service后大量_blazor请求导致应用不稳定
  1. 现象:

    • Blazor Server应用部署到Azure App Service后,浏览器Network标签出现大量指向https://mywebsites0123.azurewebsites.net/_blazor?id=3OO-T6L4Bu9bMRF8QAo_5Q&_=166722的请求,引发应用运行不稳定。
    • 本地Debug/Release模式运行时无此问题,应用流畅。
    • 已查阅Stack Overflow问题《C# Blazor server - why so many _blazor requests and such memory consumption?》,未找到有效解决方案。
  2. 应用Startup配置:

    ConfigureServices(IServiceCollection services)

    services.AddRazorPages();
    services.AddServerSideBlazor().AddMicrosoftIdentityConsentHandler();
    
    services.AddSignalR(e =>
    {
                    e.MaximumReceiveMessageSize = 2000 * 1024;
    });
    
    // Radzen init
    services.AddScoped<DialogService>();
    services.AddScoped<TooltipService>();
    services.AddScoped<NotificationService>();
    
    services.AddMemoryCache();
    
    var apiScope = Configuration["Authentication:ApiScope"];
    services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "Authentication:AzureAd")
    .EnableTokenAcquisitionToCallDownstreamApi(new string[] { apiScope })
    .AddInMemoryTokenCaches();
    
    services.AddControllersWithViews().AddMicrosoftIdentityUI();
    services.AddAuthorization();
    

    Configure(IApplicationBuilder app, IWebHostEnvironment env)

    if (env.IsDevelopment())
    {
       app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthentication();
    app.UseAuthorization();
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    });
    
解决方案
  • 启用Azure App Service的WebSockets功能(默认处于关闭状态)。Blazor Server依赖SignalR实现双向通信,SignalR在Azure App Service中优先使用WebSockets传输,若未开启WebSockets,会自动降级为长轮询模式,这正是引发大量_blazor重复请求的原因。开启WebSockets后,SignalR会维持稳定的长连接,避免频繁请求。

内容的提问来源于stack exchange,提问作者Snake Eyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:35:24