On which is running the WebService. Like the one I can get in cmd.exe > ipconfig
:
What I would like to achieve is automatic IP configuration of Kestrel, like:
.UseKestrel(opts =>
{
opts.Listen(/*LocalIPv4ActiveAddress*/, 5000);
})
So I can switch my development machines with different active network interfaces (WiFi || Ethernet) and different local network IP addresses.
First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.
Open the Start menu and type cmd to open the Command Prompt. Type ipconfig into the Command Prompt and press Enter. The tool will return a set of data that includes your IP address.
Get Local IP Address With NetworkInterface Class in C#The GetAllNetworkInterfaces() function in the NetworkInterface class gives us all the network interfaces on our local machine. The NetworkInterfaceType property in the NetworkInterface class is used to get the type of the network interface.
You can try something like this:
// order interfaces by speed and filter out down and loopback
// take first of the remaining
var firstUpInterface = NetworkInterface.GetAllNetworkInterfaces()
.OrderByDescending(c => c.Speed)
.FirstOrDefault(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up);
if (firstUpInterface != null) {
var props = firstUpInterface.GetIPProperties();
// get first IPV4 address assigned to this interface
var firstIpV4Address = props.UnicastAddresses
.Where(c => c.Address.AddressFamily == AddressFamily.InterNetwork)
.Select(c => c.Address)
.FirstOrDefault();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With