I know what the documentation says, but I do not know where the IServer gets introduced or how it is configured.
My specific case is that I am calling IHostBuilder.ConfigureWebHost (not ConfigureWebHostDefaults), which as best I can determine does not automatically include Kestrel. I am using HttpSys via a UseHttpSys instead of using Kestrel.
I ran into an issue when I ran two local development websites at the same time. Even though the lauchSettings file had different ports for each, they both did register port 5000. Of course the 2nd site received an error indicating that 5000 was already in use. After much poking around, I found documentation indicating that port 5000 was the default for everything not just Kestrel. (I really believed that prior to 5.0, only Kestrel defaulted to 5000.) I proved the defaults by explicitly setting a URL in my code and it was honored and 5000 was not accessed. I then removed the code and set "urls": "http://localhost:6000" in the appSettings file and it to was honored. At this point I tried both true and false as the parameter to PreferHostingUrls and they both worked with the url configured in the appSettings file and both failed without an explicit url in either the appSettings or code.
So part of the question becomes what is IServer and how is it introduced and configured.
Both the HostBuilder and IWebHostBuilder containing the UseUrls method, it is semicolon-delimited list of IP addresses or host addresses with ports and protocols that the server should listen on for requests. By using this method, we could set the URL that the server should listen on for requests,
Besides, when we configure the Asp.net core application to use Server (such as Http.sys or Kestrel), in the server options, we could also set the URL that the server should listen on for requests, such as using the HttpSysOptions.UrlPrefixes Property or the KestrelServerOptions.Listen() method.
Then, using the PreferHostingUrls
property, we could indicate whether the host should listen on the URLs configured on the IWebHostBuilder or those configured on the IServer.
Sample code as below:
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hostsettings.json", optional: true)
.AddCommandLine(args)
.Build();
return WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5000")
.UseConfiguration(config)
.UseHttpSys(serveroptions =>
{
serveroptions.Authentication.Schemes = Microsoft.AspNetCore.Server.HttpSys.AuthenticationSchemes.None;
serveroptions.Authentication.AllowAnonymous = true;
serveroptions.MaxConnections = 100;
serveroptions.MaxRequestBodySize = 30000000;
serveroptions.UrlPrefixes.Add("http://localhost:5001");
})
//.ConfigureKestrel(serverOptions =>
//{
// serverOptions.Limits.MaxConcurrentConnections = 100;
// serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
// serverOptions.Limits.MaxRequestBodySize = 10 * 1024;
// serverOptions.Limits.MinRequestBodyDataRate =
// new MinDataRate(bytesPerSecond: 100,
// gracePeriod: TimeSpan.FromSeconds(10));
// serverOptions.Limits.MinResponseDataRate =
// new MinDataRate(bytesPerSecond: 100,
// gracePeriod: TimeSpan.FromSeconds(10));
// serverOptions.Listen(IPAddress.Loopback, 5001);
// serverOptions.Limits.KeepAliveTimeout =
// TimeSpan.FromMinutes(2);
// serverOptions.Limits.RequestHeadersTimeout =
// TimeSpan.FromMinutes(1);
//})
.PreferHostingUrls(false)
.Configure(app =>
{
app.Run(context =>
context.Response.WriteAsync("Hello, World!"));
});
}
If the PreferHostingUrls
is false
, it listening the 5001 port:
If the PreferHostingUrls
is true
, it will listen the 5000 port:
Reference: