I am trying to figure out the correct way to change the URLs that kestrel is listening on from the constructor of the launch class .
Update. In addition to the question below, I realized that the Startup class is not used to configure Kestrel as I thought. I thought that Startup would create a single application configuration object, which will be located through the agreement, but this is not so. As @Tseng points out, application and hosting configuration is a separate issue. The related answer and the accepted answer provide working examples.
I deployed a brand new Ubuntu 14 box to Vagrant and installed ASP.NET Core 1.1 according to current instructions from Microsoft: https://www.microsoft.com/net/core#linuxubuntu
I ran:
- dotnet new -t web
- dotnet recovery
- dotnet run
This is done by default http: // localhost: 5000 . In Program.cs, I can call app.UseUrls ("http: // *: 5001), and this works to change the URL and port.
I really want to change the URL by convention, adding a new JSON settings file to the Startup class, so I followed the examples and created a hosting.JSON file with this (note: I tried "server.urls" "and" urls "as the key).
{ urls: "http://*:5001" }
In Startup.cs below the default lines to add the appsettings.json files, I added .AddJsonFile ("hosting.json", optional: false); (optional: false to ensure that it was collecting the file)
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile("hosting.json", optional: false); if (env.IsDevelopment()) {
I checked that this parameter exists after the configuration is built, but it is not taken or is not used when the host is built in Program.cs
public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } }
and the application is still starting to listen on localhost: 5000. This is my (probably incorrect) understanding that, having correctly configured the setting with the correct key, WebHostBuidler must pick it up and use it.
I saw other examples that basically get rid of the startup class and create the configuration in Program.cs, where it can then be passed to the UseConfiguration call, but again I understand that using the Startup class should do this by convention.
Essentially I would like to leave Startup and Program separate , add the host.JSON file to the configuration with the URLs, and it was picked up and used without having to call UseUrls, UseConfiguration, etc.
Am I missing something obvious or trying to do something that is actually not right?
To explain why this is not a duplicate according to my comment:
This article specifically states: "launcherSettings for Visual Studio F5." I use the command line in a Linux box. Nothing to do with VS.
The solution presented in this post transferred the configuration assembly to the main method. I specifically declare that I want to create my configuration in the Startup class, as the default project "dotnet new -t web".
I do not think this is a duplicate, and I am still looking at the source of ASP.NET Core to see if this is possible.
Regarding the correct key:
https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs
namespace Microsoft.AspNetCore.Hosting { public static class WebHostDefaults { public static readonly string ApplicationKey = "applicationName"; public static readonly string StartupAssemblyKey = "startupAssembly"; public static readonly string DetailedErrorsKey = "detailedErrors"; public static readonly string EnvironmentKey = "environment"; public static readonly string WebRootKey = "webroot"; public static readonly string CaptureStartupErrorsKey = "captureStartupErrors"; public static readonly string ServerUrlsKey = "urls"; public static readonly string ContentRootKey = "contentRoot"; } }
https://github.com/aspnet/Hosting/blob/80ae7f056c08b740820ee42a7df9eae34541e49e/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
public class WebHost : IWebHost { private static readonly string DeprecatedServerUrlsKey = "server.urls";