它正在运行webservice。就像我能进去的那个:
c# - .NET Core 2.x如何获取当前事件的本地网络IPv4地址?-LMLPHP
我想实现的是红隼的IP自动配置,比如:

.UseKestrel(opts =>
    {
        opts.Listen(/*LocalIPv4ActiveAddress*/, 5000);
    })

所以我可以用不同的主动网络接口(wifi ethernet)和不同的本地网络ip地址来切换我的开发机器。

最佳答案

你可以试试这样的方法:

// order interfaces by speed and filter out down and loopback
// take first of the remaining
var firstUpInterface = NetworkInterface.GetAllNetworkInterfaces()
    .OrderByDescending(c => c.Speed)
    .FirstOrDefault(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up);
if (firstUpInterface != null) {
    var props = firstUpInterface.GetIPProperties();
    // get first IPV4 address assigned to this interface
    var firstIpV4Address = props.UnicastAddresses
        .Where(c => c.Address.AddressFamily == AddressFamily.InterNetwork)
        .Select(c => c.Address)
        .FirstOrDefault();
}

09-10 20:55