Search code examples
c#.netasp.net-core.net-corekestrel-http-server

.NET Core 3.0 IHostedService access web server URL - scheme, host, port etc


The issue that I have is simple. I want to access the server URL in my IHostedService.

I cannot find a way to do it. There isn't any request there so I cannot use the IHttpContextAccessor.

IServer features property doesn't have any addresses and I am out of options.

I do not want to hard code the URL of the server in the configuration.

The version of .NET core that I am running is 3.0.


Solution

  • You cannot get the address/url from your server before you completely start the server.

    If you need your localhost address, look it up (programmatically or otherwise) from ./Properties/launchSettings.json.

    If you need the address in a live environment, you have to wait for the server to load.