.NET 8.0 Blazor Web App部署K8s后未发起SignalR连接求助
Blazor InteractiveServer模式部署到AKS后SignalR连接异常排查
我使用.NET 8.0 Blazor开发了一个仪表盘应用,后端通过定时器更新页面内容。本地Visual Studio中以容器或独立方式运行时功能正常,但部署到Azure Kubernetes集群(AKS)后,页面可加载初始数据,但定时器无法更新页面。查看Kubernetes日志,确认定时器正常运行且无异常输出。
本地运行时,浏览器网络标签页可见SignalR协商请求及消息交互,控制台有对应连接日志;部署到K8s后,浏览器无任何SignalR相关网络活动,控制台也无相关输出。
该应用基于Blazor Web App VS模板,采用InteractiveServer模式,集成了MS EntraID认证。当前需要排查客户端JS脚本未尝试连接_blazor SignalR hub的原因。
相关配置文件
.csproj配置
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <LangVersion>latest</LangVersion> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> </PropertyGroup> <ItemGroup> <PackageReference Include="Dapper" Version="2.1.35" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.7" NoWarn="NU1605" /> <PackageReference Include="Microsoft.Identity.Web" Version="3.0.1" /> <PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="3.0.1" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" /> <PackageReference Include="Npgsql" Version="8.0.3" /> <PackageReference Include="Seq.Api" Version="2024.3.0" /> </ItemGroup> </Project>
简化后的program.cs
builder.Services .AddRazorComponents() .AddInteractiveServerComponents() .AddHubOptions(ho => { ho.EnableDetailedErrors = true; }); builder.Services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; }); builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApp(options => { builder.Configuration.Bind("AzureAd", options); options.Events = new OpenIdConnectEvents { OnRedirectToIdentityProvider = context => { context.ProtocolMessage.RedirectUri = context.ProtocolMessage.RedirectUri.Replace("http://", "https://"); return Task.CompletedTask; } }; }); ... var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts(); app.UseDeveloperExceptionPage(); } app.UseForwardedHeaders(); app.UseAuthentication(); app.UseAuthorization(); app.UseStaticFiles(); app.UseAntiforgery(); app.MapRazorComponents<App>() .AddInteractiveServerRenderMode(); app.Run();
Dockerfile配置
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base USER app WORKDIR /app EXPOSE 8080 EXPOSE 8081 FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build ARG BUILD_CONFIGURATION=Release WORKDIR /src COPY ["Dashboard/Dashboard.csproj", "Dashboard/"] RUN dotnet restore "./Dashboard/Dashboard.csproj" COPY . . WORKDIR "/src/Dashboard" RUN dotnet build "./Dashboard.csproj" -c $BUILD_CONFIGURATION -o /app/build FROM build AS publish ARG BUILD_CONFIGURATION=Release RUN dotnet publish "./Dashboard.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Dashboard.dll"]
Ingress配置
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: systems-dashboard-ingress annotations: nginx.ingress.kubernetes.io/affinity: "cookie" nginx.ingress.kubernetes.io/session-cookie-name: "affinity" nginx.ingress.kubernetes.io/session-cookie-expires: "14400" nginx.ingress.kubernetes.io/session-cookie-max-age: "14400" nginx.ingress.kubernetes.io/proxy-http-version: "1.1" labels: app.kubernetes.io/managed-by: Helm spec: ingressClassName: nginx tls: - hosts: - {mysite} rules: - host: {mysite} http: paths: - path: / pathType: Prefix backend: service: name: systems-dashboard port: number: 8080
已做排查动作
- 已按照官方文档添加Ingress注解
- 尝试调试混淆后的
blazor.web.js,但因代码混淆难以定位问题 - 手动请求
https://{mysite}/_blazor/negotiate?negotiateVersion=1,可正常返回SignalR协商响应
内容的提问来源于stack exchange,提问作者user1890407
相关产品推荐
相关产品推荐

