Search code examples
c#.net-corecentosasp.net-core-webapikestrel

Changing hostname and port hosting .net core DLL on Linux


I'm trying to host my first service on a CentOS Linux VM.

My launchSettings.json looks like this :

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:64707",
      "sslPort": 44323
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/test/get",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Release"
      }
    },
    "TestService": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/test/get",
      "applicationUrl": "https://centos-vm:1234",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Release"
      }
    }
  }
}`

Note the hostname and port.

I publish the service with the following settings : enter image description here

I then FTP the published files to my VM, and start up the service with this :

dotnet mytestservice.dll

The service starts running, and inflating all the DLL's. However, my problem comes with this prompt :

Now listening on: http://localhost:5000

How do I specify the hostname and port, and change the connection to be secure (https)?


Solution

  • In Program.cs you have the method CreateHostBuilder, you'll need to adjust this method. By default it looks something like this:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                        webBuilder.UseKestrel();
                        webBuilder.UseIISIntegration();
                    });
    

    Here you can customize the web host. In your case to change the URL used you can use webBuilder.UseUrls("Url 1", "Url 2"...)