2

I need the listening IP+port pairs to be able to forward them through a NAT implementation. However, barring a request (which is when HttpContext becomes accessible), I'm not seeing an easy way of getting access to this information.

I noted that someone else had the same question on Determine port in asp.net core, but that went unanswered.

Anyone have a clue stick to hit me with? Thanks!

Paul S.
  • 121
  • 1
  • 13

1 Answers1

2

The IWebHost interface has a ServerFeatures property , which is an instance of IFeatureCollection . If you want to get the server address & port before a HttpContext created , you could retrieve them by :

public static void Main(string[] args)
{
    var host = CreateWebHostBuilder(args).Build();

    var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>();

    // ... use serverAddresses as you like

    host.Run();
}

Note the serverAddresses here is the Address:Port listened by the WebHost , not by IIS or nginx .

itminus
  • 23,772
  • 2
  • 53
  • 88
  • Yeah, I just need details from Kestrel itself. I'll give this a try and get back to you tomorrow. – Paul S. Sep 05 '18 at 06:36