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

Add PreferHostingUrls to WebHostBuilder and IServerAddressesFeature #992

Merged
merged 1 commit into from
Mar 30, 2017

Conversation

JunTaoLuo
Copy link
Contributor

#968

Let's see if we like this naming.

@@ -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"/>.
Copy link
Contributor

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; }
Copy link
Contributor

@cesarblum cesarblum Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it settable?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor

@cesarblum cesarblum Mar 27, 2017

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.

Copy link
Member

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()
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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()
Copy link
Contributor

@cesarblum cesarblum Mar 27, 2017

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@JunTaoLuo JunTaoLuo requested review from davidfowl and Tratcher March 27, 2017 21:57
/// 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();
Copy link
Member

@davidfowl davidfowl Mar 27, 2017

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.

Copy link
Contributor Author

@JunTaoLuo JunTaoLuo Mar 27, 2017

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()
}
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dispose?

Copy link
Contributor Author

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()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JunTaoLuo JunTaoLuo force-pushed the johluo/prefer-hosting-urls branch from 1c7cb4e to 525ed34 Compare March 28, 2017 19:18
@JunTaoLuo
Copy link
Contributor Author

JunTaoLuo commented Mar 28, 2017

cc @glennc @DamianEdwards @shirhatti for naming approval.

@JunTaoLuo JunTaoLuo requested review from glennc and shirhatti March 28, 2017 23:09
@natemcmaster
Copy link
Contributor

@JunTaoLuo can you share samples of what users code might look like?

@natemcmaster
Copy link
Contributor

I'm not sure PreferHostingUrls makes it clear what the user is configuring.

That said, naming is hard. Been thinking about this and can't come up w/ anything better. And PreferConfigurationFromIWebHostBuilderOverIServer is certainly not good.

@JunTaoLuo
Copy link
Contributor Author

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, http://localhost:5001 configured on the server is used if no address is configured by the environment variable. However, if ASPNETCORE_URLS is set, then that will override the default configured on Kestrel (once the changes in Kestrel are made).

@cesarblum
Copy link
Contributor

@natemcmaster @JunTaoLuo How about PreferWebHostBuilderUrls?

@davidfowl
Copy link
Member

davidfowl commented Mar 29, 2017

@natemcmaster @JunTaoLuo How about PreferWebHostBuilderUrls?

Downvotes are so passive aggressive. So here's some text describing the downvote. 👎

@cesarblum
Copy link
Contributor

Well, I tried :P

@natemcmaster
Copy link
Contributor

:shipit:

Add PreferHostingUrls to IServerAdressesFeature

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

Successfully merging this pull request may close these issues.

7 participants