1

My app responds to port 5000, but only responds on localhost. How can I change the code so it will response not only to localhost but something like: https://*:5000

I need it so I will be able to deploy on Kubernetes and access it on port 5000 not only with localhost.

Program.cs code below

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

namespace myWebApp
{
    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>();
                });
    }
}
Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
  • Possibly duplicate of https://stackoverflow.com/questions/37365277/how-to-specify-the-port-an-asp-net-core-application-is-hosted-on – RahulGo8u Jan 09 '20 at 18:09
  • 1
    Does this answer your question? [How to specify the port an ASP.NET Core application is hosted on?](https://stackoverflow.com/questions/37365277/how-to-specify-the-port-an-asp-net-core-application-is-hosted-on) – Heretic Monkey Jan 09 '20 at 18:12
  • He isn't asking how to specify the port. He is asking how to make listen on a different address than `localhost`. `localhost` is the appropriate address, it listens on the address it belongs to. Its the apps connecting to it that would have to specify the address of the machine or container it is running on. OP can you provide some background as to what you are actually trying to achieve? –  Jan 09 '20 at 18:16
  • I try to automatically deploy it to k8s with GitLab. Developer at GitLab asked me to change my code to support this functionality: here is the original request hmm, based on my reading it already already responds on 5000, but only responds on localhost. So you need to do something similar to what they do over here, https://blog.kontena.io/dot-net-core-and-sql-server-in-docker/ var host = new WebHostBuilder() .UseUrls("https://*:5000") .UseKestrel() – Itzik Gan-Baruch Jan 09 '20 at 18:54

4 Answers4

1

I know this is now an old question, but if anyone else is struggling with this: try deploying to 0.0.0.0:5000 instead of localhost:5000 or 127.0.0.1:5000. This will allow external connections.

Tristan Reid
  • 5,844
  • 2
  • 26
  • 31
0

Your method CreateHostBuilder builds an application that listens to/responds to requests on port 5000. It is the default configuration for Kestrel web server. But this is pretty much all the application can control.

Now if you want your application to respond to http://anUrl.com then you will have to configure the network so that the computer (running the application, inside Kubernetes) is accessible at the given URL and the request send to the 5000 port. The application configuration does not control that.

If the network is the whole Web, then you'll have to register the URL to map to your computer's IP. It is done on DNS; and usually on the website you purchased your URL.

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
0

If you want to set up your app for Kubernetes, there are very different models that can be used there. But a very common one is that you will have an ingress controller that will do all the communication from external networks to you cluster. Depending on the host in the HTTP request it will then redirect it to your pod and application, so the app won't even know how the traffic reached it.

Basically the thing is that your application will answer on port 5000 regardless of how the traffic reaches the application. It's the job for the web server (or ingress controller in standard Kubernetes) to do everything else.

I suggest that you read what does DNS mean to understand the relationship better.

P.S. Short version: Your app should not know anything about the host really, so your code is just fine

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
0

I also stumbled over a similar case when I wanted to test my Web app over my mobile phone and in my local network. The thing here is this, before any app can connect to your app, if you want it to be run and accessed in debug mode then you have to use the port which your ASP.NET application is launched with.

Else to access it over the network or with another IP address while the application is hosted on your PC then you have to host it in a web server (ie IIS, nginx, kestrel), and then you can just assign it a range of ports which you want it to be accessed over.

Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
Formula12
  • 305
  • 1
  • 3
  • 14