natemcmaster / dotnet-serve

Simple command-line HTTPS server for the .NET Core CLI

Home Page:https://nuget.org/packages/dotnet-serve/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Any idea if there is a way to disable binding to IPv6

mungojam opened this issue · comments

Thanks for the great library!

I'm trying to host something on localhost, but I get a warning about IPv6 binding failing. I only need to host on IPv4. I thought that setting the address to 127.0.0.1 might solve it, but it still seems to bind to localhost and on IPv6 as well as IPv4:

dotnet serve -d UI/build -a 127.0.0.1 -p 50055

warn: Microsoft.AspNetCore.Server.Kestrel[0]

  Unable to bind to http://localhost:50055 on the IPv6 loopback interface: 'Cannot assign requested address'.

Listening on:
http://localhost:50055

Press CTRL+C to exit

@mungojam maybe your IPv6 is not enabled/supported. And, it is the default behavior of Kestrel to bind to both IPv6 and IPv4, we can not control that. Check this issue for the same aspnet/Announcements#185

@ansariabr thanks for the link. That partly explains it, and makes sense for localhost but suggests that my workaround should work and should force it to just ipv4.

To bind to dynamic ports on both loopback interfaces, you must explicitly bind to 127.0.0.1:0 and [::1]:0.

It seems odd that I set it to bind to 127.0.0.1 but then the message is about localhost. I will try and have a go with kestrel directly to check that it does the same thing

It seems odd that I set it to bind to 127.0.0.1 but then the message is about localhost.

That would be because of this check here:

|| (Addresses.Length == 1 && IPAddress.IsLoopback(Addresses[0]));
.

Other than the warning, is there something wrong happening here?

Aha that explains it, thanks.

Other than the warning, is there something wrong happening here?

It threw me off the real problem with my script for a while because the warning made me think it was only trying to bind on ipv6 and failing so then I spent a while trying to get it to bind to ipv4 'instead' before realising that it was binding on ipv4 just fine.

Happy to close, it would be better IMO for 127.0.0.1 to just bind to ipv4, but it's pretty theoretical for my use case now as I can just ignore the warning