public async Task <RegistryInformation> RegisterServiceAsync(ServiceConfiguration serviceConfiguration, ProtocolConfiguration protocolConfiguration, Uri healthCheckUri = null, IEnumerable <string> tags = null)
        {
            var serviceId   = ServiceDiscoveryHelper.GetServiceId(serviceConfiguration.Interface, serviceConfiguration.Port.ToString());
            var serviceName = ServiceDiscoveryHelper.GetServiceName(protocolConfiguration.Protocol, serviceConfiguration.Interface);

            string versionLabel = $"{ServiceDiscoveryHelper.VERSION_PREFIX}{serviceConfiguration.Version}";
            var    tagList      = (tags ?? Enumerable.Empty <string>()).ToList();

            tagList.Add(protocolConfiguration.Protocol);
            tagList.Add(protocolConfiguration.Transmit);
            tagList.Add(versionLabel);

            var instance = new RegistryInformation
            {
                Name    = serviceName,
                Id      = serviceId,
                Address = await DnsUtil.GetIpAddressAsync(),
                Port    = serviceConfiguration.Port,
                Version = serviceConfiguration.Version,
                Tags    = tagList
            };

            ServiceInstances.Add(instance);
            return(instance);
        }
 public async Task DeregisterServiceAsync()
 {
     foreach (var serviceConfiguration in configuration.serviceConfigurations)
     {
         var serviceId = ServiceDiscoveryHelper.GetServiceId(serviceConfiguration.Interface, serviceConfiguration.Port.ToString());
         await DeregisterServiceAsync(serviceId);
     }
 }
        public async Task <List <RegistryInformation> > FindServicesAsync(string name)
        {
            var queryResult = await _consul.Health.Service(name, tag : "", passingOnly : true);

            var instances = queryResult.Response.Select(serviceEntry => new RegistryInformation
            {
                Name    = serviceEntry.Service.Service,
                Address = serviceEntry.Service.Address,
                Port    = serviceEntry.Service.Port,
                Version = ServiceDiscoveryHelper.GetVersionFromStrings(serviceEntry.Service.Tags),
                Tags    = serviceEntry.Service.Tags ?? Enumerable.Empty <string>(),
                Id      = serviceEntry.Service.ID
            });

            return(instances.ToList());
        }
        public async Task <List <RegistryInformation> > FindAllServicesAsync()
        {
            var queryResult = await _consul.Agent.Services();

            var instances = queryResult.Response.Select(serviceEntry => new RegistryInformation
            {
                Name    = serviceEntry.Value.Service,
                Id      = serviceEntry.Value.ID,
                Address = serviceEntry.Value.Address,
                Port    = serviceEntry.Value.Port,
                Version = ServiceDiscoveryHelper.GetVersionFromStrings(serviceEntry.Value.Tags),
                Tags    = serviceEntry.Value.Tags
            });

            return(instances.ToList());
        }
        public async Task <RegistryInformation> RegisterServiceAsync(ServiceConfiguration serviceConfiguration, ProtocolConfiguration protocolConfiguration, Uri healthCheckUri = null, IEnumerable <string> tags = null)
        {
            var serviceId   = ServiceDiscoveryHelper.GetServiceId(serviceConfiguration.Interface, serviceConfiguration.Port.ToString());
            var serviceName = ServiceDiscoveryHelper.GetServiceName(protocolConfiguration.Protocol, serviceConfiguration.Interface);

            string versionLabel = $"{ServiceDiscoveryHelper.VERSION_PREFIX}{serviceConfiguration.Version}";
            var    tagList      = (tags ?? Enumerable.Empty <string>()).ToList();

            tagList.Add(protocolConfiguration.Protocol);
            tagList.Add(protocolConfiguration.Transmit);
            tagList.Add(versionLabel);

            string check             = healthCheckUri?.ToString() ?? $"";
            var    agentServiceCheck = protocolConfiguration.Transmit == "tcp" ?
                                       new AgentServiceCheck {
                TCP = check, Interval = TimeSpan.FromSeconds(2)
            }
                                                    : new AgentServiceCheck {
                HTTP = check, Interval = TimeSpan.FromSeconds(2)
            };

            var registration = new AgentServiceRegistration
            {
                ID      = serviceId,
                Name    = serviceName,
                Tags    = tagList.ToArray(),
                Address = await DnsUtil.GetIpAddressAsync(),
                Port    = serviceConfiguration.Port,
                Check   = new AgentServiceCheck
                {
                    HTTP = check, Interval = TimeSpan.FromSeconds(2)
                }
            };

            await _consul.Agent.ServiceRegister(registration);

            return(new RegistryInformation
            {
                Name = registration.Name,
                Id = registration.ID,
                Address = registration.Address,
                Port = registration.Port,
                Version = serviceConfiguration.Version,
                Tags = tagList
            });
        }