在dotnet.exe下运行时,IServerAddressesFeature地址为空 [英] IServerAddressesFeature addresses empty when running under dotnet.exe

查看:684
本文介绍了在dotnet.exe下运行时,IServerAddressesFeature地址为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core 2.0 Web应用程序,在启动时我需要将该应用程序的URL传递出去,以便集成到其他内容中。

I have a ASP.NET Core 2.0 web application, where I need to pass the application's URL out during startup for the purposes of integration into something else.

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

When running under Visual Studio (IIS Express), IApplicationBuilder.ServerFeatures.Get<IServerAddressesFeature>().Addresses contains the URL my application is bound to (e.g. http://localhost:1234).

使用 dotnet.exe myapp.dll ,相同的集合为空,但是,我在stdout上显示一行,表示现在正在监听:http:// localhost:5000

When run using dotnet.exe myapp.dll, the same collection is empty, however, I get a line on stdout saying Now listening on: http://localhost:5000.

问题是,要获取URL,我是否必须解析dotnet.exe的输出以开始的行,现在侦听: ,还是有一种较不那么脆弱的方法?

The question is, to get the URL, do I have to parse the output of dotnet.exe for the line beginning Now listening on:, or is there a less fragile way?

推荐答案

这是由于2017年3月对Kestrel进行了更改。 a href = https://github.com/aspnet/Announcements/issues/224 rel = nofollow noreferrer>公告:

This is due to a change made to Kestrel in March 2017. From the announcement:


在未明确配置地址的情况下,托管不再添加默认服务器地址

在以下情况下,WebHost不再将默认服务器地址 http:// localhost:5000 添加到 IServerAddressesFeature 没有指定。默认服务器地址的配置现在将由服务器负责。

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.

IServerAddressesFeature 中指定的地址当没有直接指定显式地址时,服务器将其用作备用。

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

在< a href = https://github.com/aspnet/Hosting/issues/956 rel = nofollow noreferrer>在没有明确配置地址的情况下,托管不再添加默认服务器地址:


如果您要实现服务器并依靠 IServerAddressesFeature 进行托管设置,则不会如果没有配置地址,则设置更长的时间,并应添加默认值。例如:

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
}


这篇关于在dotnet.exe下运行时,IServerAddressesFeature地址为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆