Search code examples
asp.net-corecurlwindows-subsystem-for-linuxkestrel

WSL2 Cannot reach web app in the PC localhost Kestrel server


In my PC localhost(Win10). I start asp.net core web app in Kestrel and port is 8081.

I want use the curl command in the wsl2 to test web app and I found the issue.

In my PC the following url work find.

curl http://localhost:8081/hello    (working) 
curl http://127.0.0.1:8081/hello    (working) 
curl http://192.168.43.216:8081/hello  (working)
curl http://172.25.208.1:8081/hello  (working)

In the wsl2, I use the curl command but not work fine.

curl http://192.168.43.216:8081/hello   (not working)
curl http://172.25.208.1:8081/hello     (not working)

My PC localhost Network Card

Wifi
IPv4: 192.168.43.216
submask: 255.255.255.0
gatway: 192.168.43.1

vEthernet (WSL)
IPv4: 172.25.208.1
submask: 255.255.240.0
gatway:

Here is my asp.net core web api setting.

launchSettings.json 
"PrometheusCoreTest": {
  "commandName": "Project",
  "launchBrowser": true,  
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "applicationUrl": "http://0.0.0.0:8081"  
}

Web api Controller

[ApiController]
public class TestController
{
    [HttpGet("/hello")]
    public string Hello()
    {
        return "hello, world";
    }
}

I use netstat find 8081 port and set the firewall to allow 8081 port.

C:\WINDOWS\system32>netstat -ano | find "8081"
TCP    0.0.0.0:8081           0.0.0.0:0              LISTENING       22092
TCP    [::1]:2184             [::1]:8081             SYN_SENT        22972

But the result curl http://192.168.43.216:8081/hello in the wsl2 is still not working.

How can I reach the localhost web app by wsl2? Am I missing some settings?


Solution


  • OK. Finally I find the problem.

    Use the telnet to test port by wsl2. (It is not working.)

    > telnet 192.168.43.216 8081
    Trying 192.168.43.216...
    

    I found visual studio project firewall is blockaded. I don't know why the vs project is blockaded...
    enter image description here

    Just stop the blockaded rule and try again telnet to test port it work fine.

    ❯ telnet 192.168.43.216 8081
    Trying 192.168.43.216...
    Connected to 192.168.43.216.
    Escape character is '^]'.
    

    Use curl command work fine too.

    ❯ curl http://192.168.43.216:8081/hello
    hello, world%