aspnetcore: WebHost doesn't detect ASPNETCORE_HTTP_PORTS

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The .NET images for .NET 8 changed from using ASPNETCORE_URLS to using ASPNETCORE_HTTP_PORTS.

Older applications using the WebHost API don’t seem to pick up the new environment variable.

Expected Behavior

Applications using the WebHost API should use the new environment variables.

Or this should be documented as a breaking change.

Steps To Reproduce

using Microsoft.AspNetCore ;

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

class Startup
{
    public void Configure()
    { }
}
$ ASPNETCORE_HTTP_PORTS=8080 dotnet run --no-launch-profile
Hosting environment: Production
Content root path: /tmp/web2
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

As shown in the output, the application binds to port 5000 instead of port 8080 as set by the envvar.

Exceptions (if any)

No response

.NET Version

8.0.100-rc.1.23455.1

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 23 (23 by maintainers)

Commits related to this issue

Most upvoted comments

Apps using the old APIs which haven’t been updated.

These APIs aren’t marked obsolete and are still functional.

Some users aren’t inclined update their apps to use newer APIs until the old ones stop working.

What I said about versions is confusing. The hosting APIs introduced in 3.0 and later have been updated in 8.0 to support ASPNETCORE_HTTP_PORT. The hosting APIs from 2.1 and earlier were not updated.

The scenario of concern is when a user has not specified the ASPNETCORE_URLS and relies on the envvar that is set in the .NET container image.

Before .NET 8, ASPNET Core binds to port 80.

With the .NET 8 images, applications that use the newer WebApplication API bind to port 8080 instead of port 80 as described in https://learn.microsoft.com/en-us/dotnet/core/compatibility/containers/8.0/aspnet-port.

Applications that use the older WebHost API bind to port 5000 instead of port 80. This change in behavior is not documented.

The reason is backwards compatibility. If user was setting ASPNETCORE_URLS (in a derived image, a Kubernetes spec, …) then the 8.0 image will still respect that setting.