Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Urls not read from appsettings.json #1299

Closed
b-stime opened this issue Dec 20, 2017 · 10 comments
Closed

Urls not read from appsettings.json #1299

b-stime opened this issue Dec 20, 2017 · 10 comments

Comments

@b-stime
Copy link

b-stime commented Dec 20, 2017

See #1298 for my quick attempt at a fix via the GitHub web interface.

@b-stime
Copy link
Author

b-stime commented Dec 20, 2017

@muratg
@Tratcher

What are you exactly trying to fix? Is there a bug filed for the issue you're targeting here?

This code doesn't compile. _startup.Configure is a method, not an IConfiguration instance. I'm closing this and we can discuss it issue over at #1299.

I guess this may've already been discussed at some length* but it continues to burn new users (e.g., me)...

The issue is that you don't appear to be able to configure the webhost port via a config file or command line out of the box when you create a project via:

$ dotnet new razor

The WebHostBuilder already parses appsettings.json and the command line args and hydrates the Startup object IConfigure so my patch attempt seemed like an easy way to reduce astonishment.

* https://stackoverflow.com/a/45854704/93345
// #1148
// aspnet/KestrelHttpServer#1851

@Tratcher
Copy link
Member

[Rambling investigation]

The command line issue was fixed here: aspnet/MetaPackages#221. The workaround is here: aspnet/MetaPackages#221 (comment)

A similar workaround works for appsettings.json as well.

The new Kestrel config file support also reads from appsettings.json.
aspnet/KestrelHttpServer#1290 (comment)

So the remaining gap is reading Urls from appsettings.json. Right now it's read from the wrong config layer. Hosting builds an initial configuration and services so it has enough information to build the rest of the app, like the Environment. But it also reads Urls from this config.

var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];

The other config is built seperately and added to the app services:

var builder = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath)
.AddConfiguration(_config);
foreach (var configureAppConfiguration in _configureAppConfigurationBuilderDelegates)
{
configureAppConfiguration(_context, builder);
}
var configuration = builder.Build();
services.AddSingleton<IConfiguration>(configuration);

Option A to fix this would be to resolve the IConfiguration from the app services like how it resolves the server except as optional.

Server = _applicationServices.GetRequiredService<IServer>();

That config could be used as another entry in the fallback search, likely after the existing items to minimize the compat risk.
var urls = _config[WebHostDefaults.ServerUrlsKey] ?? _config[DeprecatedServerUrlsKey];

Or option B we change WebHostBuilder to pass in the app's config into WebHost from _context.Configuration rather than the host config, but this would affect more components and be a riskier change.

_context.Configuration = configuration;

var host = new WebHost(
applicationServices,
hostingServiceProvider,
_options,
_config,

Recommendation: Option A

@Tratcher Tratcher changed the title Urls not read from appsettings.json or command line Urls not read from appsettings.json Dec 22, 2017
@Tratcher Tratcher added the bug label Dec 22, 2017
@muratg muratg removed the bug label Jan 31, 2018
@muratg muratg added this to the 2.2.0 milestone Jan 31, 2018
@muratg
Copy link

muratg commented Jan 31, 2018

We'll reconsider this in 2.2

cc @glennc

@muratg
Copy link

muratg commented May 15, 2018

Recommendation is to set the URLs with server specific settings. We won't be making other changes here.

@muratg muratg closed this as completed May 15, 2018
@sharmilasubbiah
Copy link

Did this " Urls not read from appsettings.json or command line to Urls not read from appsettings.json" sorted ?? I am trying to make this work with no luck . Just wondering if you can help me please , thank you

@Tratcher
Copy link
Member

No, we opted not to take this change. 2.1 offers a different way to configure urls for kestrel from the config file. See the new kestrel docs. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.1&tabs=aspnetcore2x#how-to-use-kestrel-in-aspnet-core-apps

@Tratcher
Copy link
Member

The command line urls parameter did get fixed for 2.1.

@flaviofak
Copy link

It seems that the impact should be small because the host's config in used as the first config provider when creating the app's config. Why would option B be so risky?
Also, is it planned to be done in a future version? This behavior causes some problems when defining custom IServer classes, because each one should manually read the URLs configuration when you could have that information resolved for everyone.

@Tratcher
Copy link
Member

The host's config and app's config are constructed differently, and _context.Configuration is completely replaceable so it may not be based on the host's config. Changing this risks regressing apps that are working today. You can already achieve the same functionality using Kestrel's new config section.

@devinvisible
Copy link

devinvisible commented Oct 29, 2018

I wasted a lot of time on this, assuming that { "urls": "http://0.0.0.0:5050/" } would take effect if it was in the appsettings.json file. Bummer. The aforementioned doc page was also misleading to me. The Endpoint configuration section mentioned "urls host configuration key" which I assumed referred to the key-value pairs found in the appsettings.json file... but found I was wrong. Pretty frustrating.

The following appsettings.json configuration ended up working:

{
	"Kestrel": {
		"EndPoints": {
			"Http": {
				"Url": "http://0.0.0.0:5090/"
			}
		}
	}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants