I need to get dynamically value of host (including scheme) and port from public void ConfigureServices(IServiceCollection services)
. I was trying to do that through IServerAddressesFeature
, but when I tried to resolve it the IoC returned null (it is not registered so far). So, is there any other option to get this info?

- 728
- 1
- 10
- 31
-
why would you need scheme, host and port used by the webhost in your Startup `ConfigureServices(IServiceCollection)`? – smn.tino Jun 27 '18 at 09:57
-
1@smn.tino need to pass the info, while registering 3-rd party services. Any ideas? – managerger Jun 27 '18 at 10:06
-
ok, I guess I am wondering what you will use this information for. – smn.tino Jun 27 '18 at 11:09
-
@smn.tino have no idea in what extent it can help to find out an answer, but anyway, as far as I know it builds links based on the info. Any ideas? – managerger Jun 27 '18 at 11:49
2 Answers
The ConfigureServices(IServiceCollection services)
method is meant to configure your application's services as explained in the Microsoft documentation.
In there, it does not seem you can retrieve scheme, host and port information from the WebHost
directly.
Indeed, you normally create your WebHost
in Program.cs
:
var webHost = WebHost.CreateDefaultBuilder(args) // initialise web host
.UseStartup<Startup>()
.Build();
However, you may retrieve scheme, host and port used by the WebHost
by looking at the very same configuration the CreateDefaultBuilder()
is using to do so.
Indeed, looking into the WebHost
code, you can see CreateDefaultBuilder()
loads configuration from both appsettings.json
and appsettings.Development.json
to configure the WebHost
, as for example scheme, host and port. You may very well look at the same configuration data in ConfigureServices(IServiceCollection services)
and deduct the same configuration information.
If you are targeting Asp.Net Core 2.1, you can configure your endpoints in appsettings.json
file directly. You may do the same in your ConfigureServices(IServiceCollection services)
by looking into the Configuration
property in Startup.cs
.
Your Startup.cs
may look as follows:
public class Startup {
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services) {
// get http url
var httpEndpoint = Configuration["Kestrel:Endpoints:Http:Url"];
// get https url
var httpsEndpoint = Configuration["Kestrel:Endpoints:Https:Url"];
/*
* Use IConfiguration to retrieve information
* about loaded configuration.
*/
// Add framework services.
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
Take a look at the IConfiguration
Microsoft documentation page for more information about what methods you can use to read configuration data.

- 2,272
- 4
- 32
- 41
-
Wait, based on what I can tell, the http and https variables will STILL be empty. `httpEndpoint` and `httpsEndpoint` Is there another method that is called first or you are saying a hardcoded appsettings.json needs to be setup? – Tom Stickel Feb 05 '19 at 23:13
-
@TomStickel the code snippet is an example on how to retrieve the http/https url, when passing this information through an hardcoded appsettings.json file; the file structure is the same described in the MS doc: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.1&tabs=aspnetcore2x – smn.tino Feb 17 '19 at 22:05
-
you can inqect `IServer` and get to it that way. See https://stackoverflow.com/a/60791568/1454658 – James Durda Mar 21 '20 at 18:17
I needed to access the port inside ConfigureServices for a Service Fabric stateless application hosted in Kestrel.
One approach (ref) is to put the thing you were going to do in your Startup, in to where you build you WebHost, in Programs.cs (in my case the CreateServiceInstanceListeners of my Service Fabric StatelessService)
This is a snippet from what I was working on:
var endpoint = Context.CodePackageActivationContext.GetEndpoint("ServiceEndpoint");
return new WebHostBuilder()
.UseKestrel(/* SSL STUFF GOES HERE */)
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext)
.AddMvc(options =>
{
// Using the port here:
options.SslPort = endpoint.Port;
options.Filters.Add(new RequireHttpsAttribute());
}))
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))

- 1,802
- 2
- 20
- 35