0

I want to identify individual Azure app service machines so that I can record info in a database. I was thinking I would use the local ip address of the machine using the function below but I get an empty string returned back. The code works for VM's but not for app services. Any thoughts (especially if these are eventually behind a load balancer and running identical code. (any string is ok... it doesn't have to be an ip address)

    public static string GetLocalIpAddress()
    {
        UnicastIPAddressInformation mostSuitableIp = null;
        var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var network in networkInterfaces)
        {
            if (network.OperationalStatus != OperationalStatus.Up)
                continue;

            var properties = network.GetIPProperties();
            if (properties.GatewayAddresses.Count == 0)
                continue;

            foreach (var address in properties.UnicastAddresses)
            {
                if (address.Address.AddressFamily != AddressFamily.InterNetwork)
                    continue;

                if (IPAddress.IsLoopback(address.Address))
                    continue;

                if (!address.IsDnsEligible)
                {
                    if (mostSuitableIp == null)
                        mostSuitableIp = address;
                    continue;
                }

                // The best IP is the IP got from DHCP server  
                if (address.PrefixOrigin != PrefixOrigin.Dhcp)
                {
                    if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
                        mostSuitableIp = address;
                    continue;
                }
                return address.Address.ToString();
            }
        }
        return mostSuitableIp != null
            ? mostSuitableIp.Address.ToString()
            : "";
    }
Brian Rice
  • 3,107
  • 1
  • 35
  • 53
  • What's the point of all this? App Service instances should be treated as stateless VMs, you should never try to affinitize code or data. However if you _know what you're doing_, you have a `WEBSITE_INSTANCE_ID = 777VeryLongString750` environment variable you can pick up in your code to uniquely identify an instance. – evilSnobu Apr 19 '20 at 21:27
  • The current situation is I have two VM's that will be behind a load balancer... and the load balancer is going to query a healthcheck url... I want to keep in the database a status so the healthcheck url can return a non 200 status based on the value in the database causing the load balancer to take that particular machine offline... this will give me A/B testing and also I can publish these separately by taking one offline, publish and then switch by taking the other offline and publishing. – Brian Rice Apr 19 '20 at 21:46
  • Currently this is for two VM's haven't really gotten to app services yet... but I noticed the ip address wasn't being returned by the app service where I have implemented a healthcheck page – Brian Rice Apr 19 '20 at 21:47
  • You don't need any of that for App Service. For A/B testing use deployment slots (with or without traffic routing to set traffic weights for A/B). There's a load balancer that comes with the service which also does session affinity by default (you can turn that off in the portal via the `ARR Affinity` knob). – evilSnobu Apr 19 '20 at 21:52

0 Answers0