3

I feel silly asking this as there are dozens of articles on Net Core hosting, but I have tried everything and I am still having the issue.

I am attempting to change the port used by a self-hosted web service. I have altered the launchSettings.json file.

"MyService": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://*:51248",
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:51248"
    },
    "MyService": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://*:51248",
        "ASPNETCORE_ENVIRONMENT": "Release"
      },
      "applicationUrl": "http://localhost:51248"
    }

I have also attempted to set the port through direct configuration:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<MyServiceWorker>();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://*:51248");
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureWebHost(config => { config.UseUrls("http://*:51248"); })
                .UseWindowsService()
                .UseSerilog();
    }

Everything works fine if I run through Visual Studio, but if I run the executable directly, it still uses port 5000. If I run it as a Windows service, it seems to pick some random port.

I have hit dozens of web sites, but have not found a solution. Does anyone have any suggestions?

Tony
  • 63
  • 5
  • Were you already able to try the newer suggestions from https://stackoverflow.com/questions/38755516/how-to-change-the-port-number-for-asp-net-core-app? – Jedidja Dec 20 '19 at 15:55
  • is there an `appsettings.json` file where the port number is written into, maybe? – Mario Vernari Dec 20 '19 at 16:47
  • I have tried the latest suggestions (although it is sometimes hard to tell which core version they refer to). There is an appsettings.json file that I am using for some other configuration, but it does not contain port information. If I find a way to set the port in code, I will add that setting to the appsetttings.json file. – Tony Dec 20 '19 at 16:53
  • Does it do the same thing if you use `0.0.0.0` instead of `localhost` in your `applicationUrl` field? I've seen some weird behavior around this, particularly when using docker containers. –  Dec 20 '19 at 18:28
  • I tried changing to zeros. If I run as a console app it still runs on port 5000. However, I ran as a Windows service and was able to get to the right port. I am beginning to think that the port number needs to be included as a parameter when run as a console app. I will try reverting back a bunch of things to find the one setting that needs to be correct. I will report back. – Tony Dec 20 '19 at 19:11

2 Answers2

2

There can be many options, and one of them is from here,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

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

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.ConfigureKestrel(serverOptions =>
                    {
                        serverOptions.Listen(IPAddress.Any, 51248);
                    });
                });
    }
}
Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • Thanks for the response. I tried this, and I get the following warning - "Overriding address(es) 'http://*:51248'. Binding to endpoints defined in UseKestrel() instead." At least this gives me something to look for. – Tony Dec 20 '19 at 18:37
2

I feel pretty foolish, but I will post this in case it helps someone else. What I have discovered is that .UseUrls does work if the application is run as a Windows service. The launchSettings.json settings work when launching from within Visual Studio. I have not been able to change the listening port when just running as a console application.

It turns out that the problem was an artifact of the way that I was testing the application. Hopefully no one else will waste a lot of time doing the same thing.

Tony
  • 63
  • 5