Пример #1
0
#pragma warning disable CS1998
        private async ValueTask <AgentServiceRegistration> BuildRegisterationRequestAsync()
#pragma warning restore CS1998
        {
            if (registration != null)
            {
                return(registration);
            }
            var serviceId   = _serviceHelper.GetRunningServiceId();
            var serviceName = _serviceHelper.GetRunningServiceName();
            var serviceTags = _serviceHelper.GetRunningServiceTags().ToArray();

            var address = "http://localhost:5000";

            IPAddress addr = null;

            foreach (var addressString in _serverAddressesFeature.Addresses)
            {
                Logger.LogTrace($"Service discovery:Found address:{addressString}");
                if (addr != null)
                {
                    break;
                }

                address = addressString;

                if (address == null)
                {
                    continue;
                }

                if (address.StartsWith("http://+:") || address.StartsWith("https://+:"))
                {
                    address = address.Replace("+", "[::]");
                }

                if (!Uri.TryCreate(address, UriKind.Absolute, out var uri))
                {
                    continue;
                }

                if (IPAddress.TryParse(uri.DnsSafeHost, out addr))
                {
                    if (IPAddress.Loopback.Equals(addr) || IPAddress.IPv6Loopback.Equals(addr))
                    {
                        addr = null;
                        continue;
                    }
                    if (IPAddress.Any.Equals(addr) || IPAddress.IPv6Any.Equals(addr))
                    {
                        addr = IPAddress.Loopback;
                        break;
                    }
                }
                else
                {
                    addr = null;
                }
            }

            Logger.LogTrace($"Service discovery:Current IP address is : {addr}");
            Logger.LogTrace($"Service discovery:Nearest address:{address}");
            if (addr == null)
            {
                Logger.LogCritical($"Service discovery:No usable IP address.(0)");
                return(null);
            }

            if (!Uri.TryCreate(address, UriKind.Absolute, out var addressUri))
            {
                Logger.LogCritical($"Service discovery:Cannot build a usable URI.");
                return(null);
            }
            var hostNameOrAddress = string.Empty;

            if (IPAddress.Loopback.Equals(addr) || IPAddress.IPv6Loopback.Equals(addr))
            {
                //// lo(v6)
                hostNameOrAddress = Dns.GetHostName();
            }
            else
            {
                hostNameOrAddress = addr.ToString();
            }
            Logger.LogTrace($"Service discovery:Current host name is : {hostNameOrAddress}");
            Logger.LogTrace($"Service discovery:Current address uri is : {addressUri}");
            var ipEntry = await Dns.GetHostEntryAsync(hostNameOrAddress);

            Logger.LogTrace($"Service discovery:Found host name is : {ipEntry.HostName}");
            var addressListString = ipEntry.AddressList.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s).Append(","));

            Logger.LogTrace($"Service discovery:Found all addresses are : {addressListString}");
            bool isIpv6 = false;

            var ipAddress = ipEntry.AddressList
                            .Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                            .Where(ip => !IPAddress.Any.Equals(ip))
                            .Where(ip => !IPAddress.Loopback.Equals(ip))
                            .Where(ip => !IPAddress.None.Equals(ip))
                            .FirstOrDefault();

            if (ipAddress == null)
            {
                isIpv6    = true;
                ipAddress = ipEntry.AddressList
                            .Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                            .Where(ip => !IPAddress.IPv6Any.Equals(ip))
                            .Where(ip => !IPAddress.IPv6Loopback.Equals(ip))
                            .Where(ip => !IPAddress.IPv6None.Equals(ip))
                            .FirstOrDefault();
            }

            if (ipAddress == null)
            {
                Logger.LogCritical($"Service discovery:No usable IP address.(1)");
                return(null);
            }
            var host = ipAddress.ToString();

            if (isIpv6)
            {
                host = $"[{host}]";
            }

            address = $"{addressUri.Scheme}://{host}:{addressUri.Port}";

            Logger.LogTrace($"Service discovery:Try resolving address:{address}");

            if (!Uri.TryCreate(address, UriKind.Absolute, out addressUri))
            {
                Logger.LogCritical($"Service discovery:No usable IP address.(2)");
                return(null);
            }
            Logger.LogTrace($"Service discovery:The new address uri is :{addressUri}");
            var checks = _healthCheckHelper.GetHeathCheckInfo()
                         .Select(info => new AgentServiceCheck
            {
                HTTP     = address + info.Path,
                Interval = TimeSpan.FromSeconds(info.Interval),
                Status   = HealthStatus.Passing
            })
                         .ToArray();

            var request = registration = new AgentServiceRegistration
            {
                ID      = serviceId,
                Name    = serviceName,
                Address = addressUri.DnsSafeHost,
                Port    = addressUri.Port,
                Tags    = serviceTags,
                Checks  = checks
            };

            return(request);
        }