Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builder.WebHost.UseUrls does not seem to override default url #38185

Closed
Buildstarted opened this issue Nov 8, 2021 · 8 comments · Fixed by #39836
Closed

Builder.WebHost.UseUrls does not seem to override default url #38185

Buildstarted opened this issue Nov 8, 2021 · 8 comments · Fixed by #39836
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug. feature-minimal-hosting
Milestone

Comments

@Buildstarted
Copy link

Previously mentioned: #36245

Describe the bug

Builder.WebHost.UseUrls() fails to use the port/url provided

To Reproduce

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://*:8080");
var app = builder.Build();
app.Run();

Further technical details

  • ASP.NET Core version: 6.0
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: VS
  • Include the output of dotnet --info:
dotnet --info Output
Host (useful for support):
  Version: 6.0.0
  Commit:  4822e3c3aa

.NET SDKs installed:
  5.0.100 [C:\Program Files\dotnet\sdk]
  5.0.104 [C:\Program Files\dotnet\sdk]
  5.0.208 [C:\Program Files\dotnet\sdk]
  5.0.303 [C:\Program Files\dotnet\sdk]
  5.0.402 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21460.8 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
@Tratcher
Copy link
Member

Tratcher commented Nov 8, 2021

@halter73

@halter73 halter73 added feature-minimal-hosting bug This issue describes a behavior which is not expected - a bug. labels Nov 8, 2021
@halter73
Copy link
Member

halter73 commented Nov 8, 2021

This is a bug.

Certain IWebHostBuilder extension methods are unsupported and throw when they're called (e.g. builder.WebHost.UseEnvironment("{EnvironmentName}")) because it's too late to reload all the config at this point. However, we have WebApplicationOptions which can be passed to CreateBuilder(WebApplicationOptions options) to make up for that.

Server urls are bound late enough that this should work as is. Unfortunately there's a bug where stale hosting config is incorrectly overriding the urls configured with UseUrls(params string[] urls). This is what Configuration looks like on this line.

image

There are a number of workarounds depending on your requirements. The easiest is likely passing in the url argument to WebApplication.Run(string? url = null).

app.Run("http://*:8080");

@rafikiassumani-msft rafikiassumani-msft added this to the .NET 7 Planning milestone Nov 8, 2021
@davidfowl
Copy link
Member

I think we should also patch this in 6.0.x

@halter73
Copy link
Member

halter73 commented Nov 9, 2021

I agree it should be patched. I almost milestoned it myself, but figured we could discuss in triage.

@halter73 halter73 modified the milestones: .NET 7 Planning, 6.0.x Nov 9, 2021
@tehmufifnman
Copy link

Oof... so glad I came across this! I spent a whole day trying to figure out why my service wasn't working in my container and this was 100% it haha!

@tetious
Copy link

tetious commented Nov 17, 2021

For me, trying to override the URL in Run results in the following Changing the URL is not supported because no valid IServerAddressesFeature was found. when using WebApplicationFactory for testing. I'm uncertain if this is a mistake in my configuration/usage or a separate bug...

Setting ENV ASPNETCORE_URLS=http://+:5050 within the runtime image section in our Dockerfile worked, however, and doesn't break testing.

@tehmufifnman
Copy link

For me, trying to override the URL in Run results in the following Changing the URL is not supported because no valid IServerAddressesFeature was found. when using WebApplicationFactory for testing. I'm uncertain if this is a mistake in my configuration/usage or a separate bug...

Setting ENV ASPNETCORE_URLS=http://+:5050 within the runtime image section in our Dockerfile worked, however, and doesn't break testing.

Do you have a code snippet to share?

goncalo-oliveira added a commit to goncalo-oliveira/faas-aspnet-template that referenced this issue Dec 23, 2021
@OronDF343
Copy link

This issue also affects setting the Urls property in appsettings.{env}.json. This breaks a lot of our ASP.NET Core 3.1 projects when run in a development environment since they won't be accessible on the LAN (for some reason, launchsettings.json does not support listening on all IPs, only a specific one or localhost).

Works in 3.1 (listens on http://[::]:4321), does not work in 6.0 (listens on http://localhost:4321):

{
    "Urls": "http://*:4321"
}

Workaround for 6.0 (listens on http://[::]:4321):

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:4321"
      }
    }
  }
}

@halter73 halter73 linked a pull request Feb 5, 2022 that will close this issue
10 tasks
@halter73 halter73 closed this as completed Feb 5, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 7, 2022
dougbu pushed a commit to dougbu/aspnetcore that referenced this issue Mar 17, 2022
…lt hosting config

## Description

Prior to this change, default config (typically loaded from `DOTNET_`/`ASPNET_` environment variables and command line arguments) could override the application-level configuration. This would prevent `GenericWebHostService` from seeing the latest configuration set by `UseUrls()` of `DOTNET_URLS`, `ASPNET_URLS` or `--urls` was set.

Fixes dotnet#38185

## Customer Impact

This is a big gotcha to customers using `WebApplicationBuilder` (which is used in all the ASP.NET Core 6 templates) who expect the following to work:

```C#
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://*:8080");
var app = builder.Build();
app.Run();
```

A comment on the issue suggesting we patch this has gotten 5 thumbs ups not counting mine.

## Regression?

- [ ] Yes
- [x] No

## Risk

- [ ] High
- [ ] Medium
- [x] Low

This is a small well tested change which only affects the loading of default config sources and those added via a `HostFactoryResolver` to host configuration. [Here’s the existing test](https://github.com/dotnet/aspnetcore/blob/0f6f649f1da658bbe37b8898df0c80c5affa9d2d/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs#L883) showing that the expected configuration providers are still dispose.

## Verification

- [x] Manual (required)
- [x] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [x] N/A
mmitche added a commit that referenced this issue Apr 13, 2022
* [internal/release/6.0] Update dependencies from dnceng/internal/dotnet-efcore

* [internal/release/6.0] Update dependencies from dnceng/internal/dotnet-efcore

* [internal/release/6.0] Update dependencies from dnceng/internal/dotnet-efcore

* [internal/release/6.0] Update dependencies from dnceng/internal/dotnet-runtime

* Merged PR 21649: [internal/release/6.0] Make UseUrls() override default hosting config

## Description

Prior to this change, default config (typically loaded from `DOTNET_`/`ASPNET_` environment variables and command line arguments) could override the application-level configuration. This would prevent `GenericWebHostService` from seeing the latest configuration set by `UseUrls()` of `DOTNET_URLS`, `ASPNET_URLS` or `--urls` was set.

Fixes #38185

## Customer Impact

This is a big gotcha to customers using `WebApplicationBuilder` (which is used in all the ASP.NET Core 6 templates) who expect the following to work:

```C#
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://*:8080");
var app = builder.Build();
app.Run();
```

A comment on the issue suggesting we patch this has gotten 5 thumbs ups not counting mine.

## Regression?

- [ ] Yes
- [x] No

## Risk

- [ ] High
- [ ] Medium
- [x] Low

This is a small well tested change which only affects the loading of default config sources and those added via a `HostFactoryResolver` to host configuration. [Here’s the existing test](https://github.com/dotnet/aspnetcore/blob/0f6f649f1da658bbe37b8898df0c80c5affa9d2d/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs#L883) showing that the expected configuration providers are still dispose.

## Verification

- [x] Manual (required)
- [x] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [x] N/A

* [internal/release/6.0] Update dependencies from dnceng/internal/dotnet-runtime

* Merge from github release/6.0

* [internal/release/6.0] Update dependencies from dnceng/internal/dotnet-efcore dnceng/internal/dotnet-runtime

 - Set to private runtime

* Merged PR 21869: Correct `--architecture` in `fpm` commands

# {PR title}

Correct `--architecture` in `fpm` commands

## Description

Update `fpm` commands to use a supported `--architecture` value. As-is, the x64 .rpm files we produce are incompatible w/ installation on an x64 machine. Problem found during servicing version flow.

## Customer Impact

Unable to build dotnet-installer-ci-official w/ current .rpm files from dotnet-aspnetcore repo.

## Regression?

- [x] Yes
- [ ] No

This is a build regression since 6.0.3. It will also impact our 'main' branch.

## Risk

- [ ] High
- [ ] Medium
- [x] Low

Have verified the chosen values for the command line using available online documentation. Since we know the current .rpm files are busted, things can only get better 😃

## Verification

- [x] Manual (required)
- [ ] Automated

I am locally building this branch and will use `rpmlint` in a `docker` container to verify the goodness of the produced package.

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [ ] N/A

Not sure how to answer this because the change is to an internal setting of the RPM installer. If others agree it's the right change, it's been reviewed 😃

----

## When servicing release/2.1

- [ ] Make necessary changes in eng/PatchConfig.props

* Merged PR 21931: Revert "[release/6.0] Build ProjectTemplates in Source-Build (#40650)" (#40805)

# {PR title}

Summary of the changes (Less than 80 chars)

## Description

{Detail}

Fixes #{bug number} (in this specific format)

## Customer Impact

{Justification}

## Regression?

- [ ] Yes
- [ ] No

[If yes, specify the version the behavior has regressed from]

## Risk

- [ ] High
- [ ] Medium
- [ ] Low

[Justify the selection above]

## Verification

- [ ] Manual (required)
- [ ] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [ ] N/A

----

## When servicing release/2.1

- [ ] Make necessary changes in eng/PatchConfig.props

Revert "[release/6.0] Build ProjectTemplates in Source-Build (#40650)" (#40805)

This reverts commit 7c2000d.

Co-authored-by: dotnet-bot <[email protected]>
Co-authored-by: DotNet Bot <[email protected]>
Co-authored-by: Stephen Halter <[email protected]>
Co-authored-by: mmitche <[email protected]>
Co-authored-by: Doug Bunting (AAPT) <[email protected]>
@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug. feature-minimal-hosting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants