-
Notifications
You must be signed in to change notification settings - Fork 307
Add PreferHostingUrls to WebHostBuilder and IServerAddressesFeature #992
Conversation
@@ -47,6 +48,12 @@ public interface IWebHostBuilder | |||
IWebHostBuilder UseSetting(string key, string value); | |||
|
|||
/// <summary> | |||
/// Indicate that the host should listen on the urls configured on the <see cref="IWebHostBuilder"/> instead of those configured on the <see cref="IServer"/>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URLs
@@ -8,5 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Server.Features | |||
public interface IServerAddressesFeature | |||
{ | |||
ICollection<string> Addresses { get; } | |||
|
|||
bool PreferHostingUrls { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it settable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hosting needs to set it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the concrete type not the interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed this. The problem is that the feature is set by the server. Hosting sets it on the interface. Would have to cast to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see
@@ -67,11 +67,13 @@ public void UseStartupThrowsWithNull() | |||
} | |||
|
|||
[Fact] | |||
public void NoDefaultAddressesIfNotConfigured() | |||
public void NoDefaultAddressesAndDoNotPreferHostingUrlsIfNotConfigured() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are orthogonal. Add a new test for PreferHostingUrls
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just making sure the default is false. I will add the check for the new flag in the other tests where only address is configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just making sure the default is false
I understand, but it should be in its own test.
@@ -121,6 +123,31 @@ public void UsesNewConfigurationOverLegacyConfigForAddresses() | |||
} | |||
|
|||
[Fact] | |||
public void DoNotPreferHostingUrlsWhenNoAddressConfigured() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test with address configured but PreferHostingUrls()
not called.
public void DoNotPreferHostingUrlsWhenNoAddressConfigured() | ||
{ | ||
var host = CreateBuilder().UseServer(this).PreferHostingUrls().Build(); | ||
host.Start(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you dispose host
in all these tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, though we never had a problem with it since these are using a fake server, it doesn't consume any ips/ports. The hosts will be GC'ed and disposed automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it's mostly for consistency - I see other tests disposing it. Not sure all of them do though.
/// Indicate that the host should listen on the URLs configured on the <see cref="IWebHostBuilder"/> instead of those configured on the <see cref="IServer"/>. | ||
/// </summary> | ||
/// <returns>The <see cref="IWebHostBuilder"/>.</returns> | ||
IWebHostBuilder PreferHostingUrls(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it an extension method that sets a configuration change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if we flow this through configuration which exposes it to things like environment variables, ... I was originally gonna put this option there but I thought we explicitly didn't want this.
@@ -67,15 +67,19 @@ public void UseStartupThrowsWithNull() | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reformatting makes these tests hard to review. Was it needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We weren't disposing the hosts created during tests. It shouldn't be a problem but @CesarBS suggested to make it consistent and dispose. Hence the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those sorts of changes make it super hard to review... Next time do it later 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@natemcmaster That doesn't even help a little bit.
[Fact] | ||
public void DoNotPreferHostingUrlsWhenNoAddressConfigured() | ||
{ | ||
var host = CreateBuilder().UseServer(this).PreferHostingUrls(true).Build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dispose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops missed one
@@ -67,15 +67,19 @@ public void UseStartupThrowsWithNull() | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1c7cb4e
to
525ed34
Compare
cc @glennc @DamianEdwards @shirhatti for naming approval. |
@JunTaoLuo can you share samples of what users code might look like? |
I'm not sure That said, naming is hard. Been thinking about this and can't come up w/ anything better. And |
What would you like to see in a sample? I would imagine the default would look like the following public static void Main(string[] args)
{
var host = new WebHostBuilder()
.PreferHostingUrls(true)
.UseKestrel(options => options.Listen(IPAddress.Loopback, 5001))
.UseContentRoot(Directory.GetCurrentDirectory())
.useStartup<Startup>()
.Build();
host.Run();
} This way, |
@natemcmaster @JunTaoLuo How about |
Downvotes are so passive aggressive. So here's some text describing the downvote. 👎 |
Well, I tried :P |
|
Add PreferHostingUrls to IServerAdressesFeature Add extension to IWebHostBuilder to set this flag
525ed34
to
4cdc970
Compare
#968
Let's see if we like this naming.