为Ocelot API网关配置WatchDog.Net时仪表盘刷新报错求助
问题解决:Ocelot + WatchDog.Net 仪表盘加载报错
核心问题:中间件顺序错误
你的代码中UseWatchDog与UseOcelot的执行顺序颠倒了。Ocelot作为API网关需要优先处理请求路由,WatchDog才能正确捕获经过网关的请求数据;反之会导致WatchDog无法拦截Ocelot转发的请求,最终引发数据库加载数据报错。
修正后的Program.cs代码片段
builder.Services.AddEndpointsApiExplorer(); //.... builder.Services.AddWatchDogServices(); var app = builder.Build(); //.... await app.UseOcelot(); // 先执行Ocelot路由处理 app.UseWatchDogExceptionLogger(); app.UseWatchDog(opt => { opt.WatchPageUsername = "admin"; opt.WatchPagePassword = "admin"; }); await app.RunAsync();
补充说明
- ASP.NET Core中间件按代码书写顺序从上到下执行,必须让Ocelot先完成请求的路由转发,再由WatchDog中间件捕获请求、响应及异常信息。
- 若存在其他自定义中间件,需将WatchDog相关中间件放在Ocelot之后,确保能完整捕获所有经过网关的流量数据。
内容的提问来源于stack exchange,提问作者Ali Naeimi
相关产品推荐
相关产品推荐

