gpt4 book ai didi

asp.net-core - 在 dotnet.exe 下运行时,IServerAddressesFeature 地址为空

转载 作者:行者123 更新时间:2023-12-01 23:38:00 24 4
gpt4 key购买 nike

我有一个 ASP.NET Core 2.0 Web 应用程序,我需要在启动期间传递应用程序的 URL,以便集成到其他应用程序中。

在 Visual Studio (IIS Express) 下运行时,IApplicationBuilder.ServerFeatures.Get<IServerAddressesFeature>().Addresses包含我的应用程序绑定(bind)到的 URL(例如 http://localhost:1234 )。

当使用 dotnet.exe myapp.dll 运行时,同一个集合是空的,但是,我在标准输出上看到一行 Now listening on: http://localhost:5000 .

问题是,要获取 URL,我是否必须解析 dotnet.exe 的输出以获取以 Now listening on: 开头的行? ,或者有没有更不脆弱的方法?

最佳答案

这是由于 2017 年 3 月对 Kestrel 进行的更改所致。来自 announcement :

Hosting no longer adds default server address when no address is explicitly configured

The WebHost will no longer add the default server address of http://localhost:5000 to the IServerAddressesFeature when none is specified. The configuration of the default server address will now be a responsibility of the server.

Addresses specified in IServerAddressesFeature are intended to be used by servers as a fallback when no explicit address is specified directly.

Hosting no longer adds default server address when no address is explicitly configured中有一个如何处理这个问题的例子。 :

If you are implementing a server and rely on the IServerAddressesFeature to be set by hosting, that will no longer be set and a default should be added when no address is configured. As an example:

var serverFeatures = featureCollection.Get<IServerAddressesFeature>();
if (serverFeatures .Addresses.Count == 0)
{
ListenOn(DefaultAddress); // Start the server on the default address
serverFeatures.Addresses.Add(DefaultAddress) // Add the default address to the IServerAddressesFeature
}

关于asp.net-core - 在 dotnet.exe 下运行时,IServerAddressesFeature 地址为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48546843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com