asp.net5 selfhosted WebListener connection timeout

2019-09-02 04:25发布

问题:

I am using asp.net5 web project hosted in console app. It works fine on localhost, but when i try to access this website form different machine like http://192.168.1.5:5432 etc., i get ERR_CONNECTION_TIMED_OUT. I was trying several arouches like chaning localhost to machine name but none of them works.

hosting.ini file:

server=Microsoft.AspNet.Server.WebListener
server.urls=http://localhost:5432

Is there any solution for this problem, or hosting in console app is only made for development purposes

回答1:

Because you have bound your listener to localhost it will only accept connections to (and from) localhost. localhost is special here as it will (at least for Kestrel) always bind to localhost/loopback interface. Everything other than localhost binds to all network interfaces.

If you want it to be reachable by all IPs and be host/domain agnostic, you have to use http://0.0.0.0:5432 (or alternatively http://*:5432). If you do http://www.example.com:5432, then it will be reachable from all IPs but only if the host name was typed in the browser so http://192.168.1.5:5432 still won't work (unable to test the last one right now, but * wild card url should still work).

This way you can run multiple asp.net core applications on the same server but but with different hosts/domains.

You can also run two applications on the same domain and same port, if you bind them to different endpoints.

For example http://www.example.com/App1 and http://www.example.com/App2, use same port and same domain but different endpoints and two applications.

Edit

Addition information for windows users. You may have to add an exception to the change the local security settings to allow dnx to bind to this port/host (or run it as Admin which would be discouraged), especially when using host and low-numbered ports (80 and 443 respectively).

netsh http add iplisten ipaddress=0.0.0.0:80
netsh http add urlacl url=http://+:80/ user=Example\Username

This will allow applications for the user "Username" in the domain "Example" to start listening on port 80.