Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I feel like I'm missing something on this code jaunt. Let me walk you thru this.

In C# I'm using very simple code. There is no launchsettings.json , just hard coding it to see if this works. Here are the settings and main, just so you can see how it all starts up:

public class Program
    public static HttpListener listener;
    public static string url = "http://localhost:8001/";
---- later on in main:------
public static void Main(string[] args)
        listener = new HttpListener();
        listener.Prefixes.Add(url);
        listener.Start();
        Console.WriteLine("Listening for connections on {0}", url);

I know that the code works. When I run in VS2022 Debug mode (no docker) it shows my simple html page that says 'System okay'. If I run it in docker, this is where it all gets weird. I'll walk thru the docker config now.

DockerFile:

FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["xevenListens.csproj", "."]
RUN dotnet restore "./xevenListens.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "xevenListens.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "xevenListens.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
EXPOSE 8001
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "xevenListens.dll"]

Then I pull everything with Docker-compose.yml:

version: '3'
services:
  xevenlistens:
    image: danregalia/xevenlistens:latest
    network_mode: "host"
    container_name: x7Listens
    ports:
      - "8001:8001"

So, in my logic when I compose that up in VSCode, and it shows up in docker desktop, I get the app as expected. I inspect it, and I see this:

When I go to Terminal Tab, run curl against the localhost:8001 I get the correct response, so I know that is working:

Now I know I'm mapping that right. From the docker file, and the pictures above, you can tell that I've got port 8001 mapped across as it should be, and from the dockerfile I have 8001 exposed.

At this point, I feel confident enough that if I run http://localhost:8001 from my browser, I should that same simple html message back... right?
Wrong...

So, now I'm at a total loss as to why this isn't working. The only thing I can think of right now, is that my wsl is not on localhost or 127.0.0.1 or 192.168.0.22 (my eth0 ipv4 on my computer)

I've traced this thru the best I can think of and read thru 6 other similar posts.

The problem is here - public static string url = "http://localhost:8001/"; it will accept requests only from localhost which for case of dockerized application will be the container itself. Change localhost to + (or *):

public static string url = "http://+:8001/";

From the HttpListener docs:

When a port is specified, the host element can be replaced with * to indicate that the HttpListener accepts requests sent to the port if the requested URI does not match any other prefix. For example, to receive all requests sent to port 8080 when the requested URI is not handled by any HttpListener, the prefix is http://*:8080/. Similarly, to specify that the HttpListener accepts all requests sent to a port, replace the host element with the + character. For example, https://+:8080. The * and + characters can be present in prefixes that include paths.

Wow... you nailed it... 2 days of smashing my head into keys trying to trace down everything I can think of.. and it's something dumb.. old code vs new spec. Thanks @Guru-Stron. You have no idea how much I appreciate this. – Dregalia Apr 7 at 22:08

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.