Home > Mobile >  How to override default ports in Kestrel
How to override default ports in Kestrel

Time:04-20

i have simple method

public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>());
        }

and i am write appsettings:

"Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http1AndHttp2"
    },
    "Endpoints": {
      "Api": {
        "Url": "https:// :5005",
        "Protocols": "Http1AndHttp2"
      },
      "Grpc": {
        "Url": "http:// :5006",
        "Protocols": "Http2"
      }
    }
  }

but when i start app, i see warning:

"Microsoft.AspNetCore.Server.Kestrel", "message": "Overriding address(es) 'https:\/\/localhost:5000, https:\/\/localhost:5001'. Binding to endpoints defined in UseKestrel() instead.", "addresses": "https:\/\/localhost:5000, https:\/\/localhost:5001
", "methodName": "UseKestrel()" }

and app start at standard port 5000, but i expect on port 5005

Why Kestrel changes the start port and how to make the application start on a given https://localhost:5005/api-docs/someService (i am use swagger)

CodePudding user response:

As said on your application startup just do it in UseKestrel()

return Host.CreateDefaultBuilder(args)
       .UseServiceProviderFactory(new AutofacServiceProviderFactory())
       .ConfigureWebHostDefaults(builder => builder.UseKestrel(x=>x.Listen(IPAddress.Any, 5005)).UseStartup<Startup>());

CodePudding user response:

There are the several ways you can set the port in Kestrel endpoints

Configure

Following the node to set the URL for your project.

Kestrel / EndPoints / Http / Url

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http:// :5005"
      }
    }
  }
}

Run dotnet command-line with argument --urls

dotnet yourdll.dll --urls http:// :5005

Note

But I would suggest you add Nginx or Certbot to be reverse proxy instead of setting port and expose to outside connection.

  • Related