删除 Kestrel 绑定警告

Removing Kestrel binding warning

在 ASP.NET 核心 2.1 项目中使用 Kestrel 并在 UseKestrel() 中指定绑定时,将在警告级别记录一条消息:

Overriding address(es) 'http://localhost:50000/'. Binding to endpoints defined in UseKestrel() instead.

这给我们的日志增加了干扰,报告了默认值(因此令人困惑)URL,不应作为警告。除了通过记录器本身过滤消息外,有没有办法配置 Web 主机构建器,使其在启动时不记录此信息?

首先,如果您认为在应用程序启动时只收到一次,那么“噪音”并不是真正的噪音。因此,除非您在需要重新启动应用程序的地方做了一些奇怪的事情,否则与所有其他(嘈杂得多的)消息相比,您可能几乎永远不会在日志中看到该行。

话虽这么说,但它实际上是一个有用的警告,因为它告诉您已在多个位置配置了绑定 URL。因此,正确的做法不是忽略该消息,而是实际删除重复的配置。

在这种情况下,您使用带有 UseKestrel() 的显式侦听选项,因此胜过一切。所以你应该删除其他配置。您应该查看几个位置:

  • ASPNETCORE_URLS 环境变量。
  • 遗留 ASPNETCORE_SERVER.URLS 环境变量。
  • Properties/launchSettings.json.

有同样有点烦人的警告并检查了环境变量和配置文件,但无法找到 url 列表的来源。最后,我使用 IWebHostBuilder.UseUrls() 和一个空的 url 列表来消除警告。

 IWebHostBuilder builder = new WebHostBuilder()
   .UseUrls()
   .UseKestrel()
   .ConfigureKestrel(...

也可能是launchsettings.jsonappsetings.json[=12中包含的配置冲突=]

就我而言,在本地开发时,appsetings.json 中的 Kestrel 条目:

  "kestrel": {
    "endpoints": {
      "http": {
        "url": "http://localhost:5000"
      },
      "https": {
        "url": "https://localhost:5001"
      }
    }

launchsettings.json 中的配置文件冲突:

"MyProject-Dev": {
  "commandName": "Project",
  "launchBrowser": true,
  "applicationUrl": "http://localhost:5000;https://localhost:5001",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

我认为正确的位置是在 launchsettings.json 文件中,因此删除 appsettings.json解决了我的问题。

这是一个老问题,但你可以解决 launchSettings.jsonappSettings.json 之间的冲突 "externalUrlConfiguration": true

appSettings.json中的 Kestrel 配置:

"Kestrel": {
"EndpointDefaults": {
  "Protocols": "Http1AndHttp2"
},
"Endpoints": {
  "HTTPS": {
    "Url": "https://localhost:4433"
  }
}

这是launchSettings.json内容:

{
  "profiles": {
    "TestApplication": {
      "commandName": "Project",
      "launchBrowser": true,
      "externalUrlConfiguration": true, //add this line
      "applicationUrl": "https://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}