I'm working with an ASP.NET Core 1 RTM web app and I'm updating Kestrel setup to latest conventions. Setup aims at having the following sources for server.urls
, from lowest to highest priority:
Program.Main()
code (default, e.g. for production)hosting.Development.json
(e.g. to override default while developing)As per latest references (e.g. here on SO and here on Github), this is what I got now:
ProjDir\Program.cs
:
public class Program
{
// Entry point for the application
public static void Main(string[] args)
{
const string hostingDevFilepath = "hosting.Development.json";
const string environmentVariablesPrefix = "ASPNETCORE_";
string currentPath = Directory.GetCurrentDirectory();
var hostingConfig = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile(hostingDevFilepath, optional: true)
.AddEnvironmentVariables(environmentVariablesPrefix)
.Build();
System.Console.WriteLine("From hostingConfig: " +
hostingConfig.GetSection("server.urls").Value);
var host = new WebHostBuilder()
.UseUrls("https://0.0.0.0")
.UseConfiguration(hostingConfig)
.UseKestrel()
.UseContentRoot(currentPath)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
ProjDir\hosting.Development.json
:
{
"server.urls": "http://localhost:51254"
}
From command line, having set ASPNETCORE_ENVIRONMENT=Development
, this is the output:
> dotnet run
Project Root (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
From hostingConfig: http://localhost:51254
info: AspNet.Security.OpenIdConnect.Server.OpenIdConnectServerMiddleware[0]
An existing key was automatically added to the signing credentials list: <<yadda yadda yadda>>
Hosting environment: Development
Content root path: <<my project root dir>>
Now listening on: https://0.0.0.0:443
Application started. Press Ctrl+C to shut down.
My expected output would be instead Now listening on: http://localhost:51254
.
URLs value is correctly picked up from JSON source (as per console log), but then Kestrel configuration ignores that, even if UseConfiguration
comes after UseUrls
.
What am I missing? Thanks for your suggestions.
答案 0 :(得分:2)
Try using urls
instead of server.urls
. The name of the setting changed post RC2.
答案 1 :(得分:1)
Did some more tests. It seems to me that as soon as UseUrls()
is present, no matter in which order, all Json config sources are ignored.
So I tried to come up with a solution supporting more than one hosting.json
file, e.g. a default one and then one per environment. Basically I tried to replicate in Program.Main()
a behavior similar to Startup.Startup(IHostingEnvironment env)
, where one can use both "appsettings.json"
and $"appsettings.{hostingEnv.EnvironmentName}.json"
as source. The only issue is that in Program.Main() there's no IHostingEnvironment available, but this GH issue reminded me that we still have Environment.GetEnvironmentVariable("some-variable")
in our toolbelt.
Here's the full solution, please feel free to suggest improvements or (even better) some semplification:
public class Program
{
// Entry point for the application
public static void Main(string[] args)
{
const string environmentVariablesPrefix = "ASPNETCORE_";
string hostingEnvironmentKey = $"{environmentVariablesPrefix}ENVIRONMENT";
string hostingEnvironmentValue;
try
{
hostingEnvironmentValue = Environment
.GetEnvironmentVariable(hostingEnvironmentKey);
}
catch
{
hostingEnvironmentValue = "Development";
}
const string hostingFilepath = "hosting.json";
string envHostingFilepath = $"hosting.{hostingEnvironmentValue}.json";
string currentPath = Directory.GetCurrentDirectory();
var hostingConfig = new ConfigurationBuilder()
.SetBasePath(currentPath)
.AddJsonFile(hostingFilepath, optional: true)
.AddJsonFile(envHostingFilepath, optional: true)
.AddEnvironmentVariables(environmentVariablesPrefix)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(hostingConfig)
.UseKestrel()
.UseContentRoot(currentPath)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
// in hosting.json
{
"server.urls": "https://0.0.0.0"
}
// in hosting.Development.json
{
"server.urls": "http://localhost:51254"
}