Microsoft.AspNetCore.Hosting.Diagnostics 导致 Application Insights 中频繁出现 499 错误

问题描述 投票:0回答:1

我们最近将我们的中央 API 从 4.8 项目重建为 Net8。两种服务都使用部署槽并行运行,流量按 50/50 分配,要么到达原始路由,要么到达新的 Net8 构建。 (为了澄清,决定不使用 MS 迁移工具,只是从文件/新的 net 8 项目重建)。

在过去几天中,Application Insights 显示 API 基本 URL 出现大量故障。

我们最初假设应用程序服务上的 KeepAlive 存在问题,但通过查看日志和文档,这些请求的频率对于 Keep Alive 来说太高了,当我观察应用程序日志流时,可以轻松识别特定事件并返回 200。

最终我们确定请求源来自应用程序服务自己的本地主机,并进一步调查在日志流中发现了此事件:

2024-02-09 11:02:56.635 +00:00 [Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request finished HTTP/1.1 GET [BASE URL OF THE API]/ - 499 - - 0.7112ms

这个似乎与NET8相关,我从网上看到的唯一东西是这个github票证,与中间件有关,但我们没有使用相同的中间件/没有使用UseExceptionHandler或UseTimeout。这是我参考的票证:https://github.com/dotnet/aspnetcore/issues/52295

是否有人能够就如何阻止这些呼叫或至少配置服务以更适当地处理它们提供任何建议或经验?

我们确实认为这可能是因为 / 处没有任何内容,但现在我们返回一个空页面,这与 Keep Alive 配合得很好。如果相关的话,我们不会使用最小的 API。

提前致谢。

c# asp.net-core asp.net-web-api azure-application-insights .net-8.0
1个回答
0
投票

为了澄清,决定不使用 MS 迁移工具,只是从文件/新的 net 8 项目重建。

在这里,我创建了一个针对 .NET 8 的新 ASP.NET Core 项目进行测试,并按照此链接

在应用程序中配置了基本 URL

程序.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApplication15
{
    public static class HttpRequestExtensions
    {
        public static string? BaseUrl(this HttpRequest req)
        {
            if (req == null) return null;
            var uriBuilder = new UriBuilder(req.Scheme, req.Host.Host, req.Host.Port ?? -1);
            if (uriBuilder.Uri.IsDefaultPort)
            {
                uriBuilder.Port = -1;
            }

            return uriBuilder.Uri.AbsoluteUri;
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddControllers();
            builder.Services.AddHttpContextAccessor(); // Add HttpContextAccessor

            var app = builder.Build();

            if (app.Environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            app.Run();
        }
    }
}
  • 我还配置了部署槽,以通过向端点发出请求来生成到我的 API 的流量,从而在 API 的新旧版本之间实现流量拆分。

  • 对于 499 错误,请对超时设置进行一些调整、修改中间件配置、优化请求处理逻辑或更改部署设置。

我的应用程序没有失败问题,我一开始也只遇到无效操作异常。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.