Search code examples
c#asp.net-coreasp.net-core-webapi

ASP.NET Core absolutely refuses to host on other port


I am running a ASP.NET Core 8.0 Web Service application on arm64. On my Windows computer, I develop the app and publish it to arm64, and copy it to the target computer (a raspberry pi). Despite every configuration file demanding that the app be hosted on port 11111(which I want), when I start the application on the target computer, this happens:

warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'http://*.11111'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
fail: Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      System.IO.IOException: Failed to bind to address http://[::]:80: address already in use.

I've tried everything with multiple redundancies and I can't seem to get it to host on 11111.

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "urls": "http://*.11111;http://192.168.1.169:11111;http://localhost:11111;http://<MYDOMAIN>:11111;http://*:11111"
}

Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Constants.LoadConstants();
builder.WebHost.UseUrls(new string[]
{
    "http://*.11111",
    "http://192.168.1.169:11111",
    "http://<MYDOMAIN>:11111",
    "http://0.0.0.0:11111",
    "http://*:11111"
});
builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = null;
});

var app = builder.Build();

// Configure the HTTP request pipeline.
app.Urls.Add("http://*.11111");
app.MapControllers();

app.Run();

The name "80" is not mentioned at all in the entire app. Any help is much appreciated.


Solution

  • Your app says

    Overriding address(es) 'http://*.11111'

    but this is not a valid url, so the default port is used. It should be written with a colon instead of the dot. This line does the trick for me:

    builder.WebHost.UseUrls("http://*:11111");
    

    The appsettings and app.Urls.Add is not needed.