public bool TryAdd(string name, ServiceChangeToken token) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (token == null) { throw new ArgumentNullException(nameof(token)); } return(_tokens.TryAdd(name, token)); }
private IEnumerable <ServiceEndPoint> RemoteServiceEndpoint(string name) { var client = ConsulClientFactory.CreateConsulClient(_options); var queryOptions = QueryOptions.Default; queryOptions.Consistency = ConsistencyMode.Stale; if (!string.IsNullOrEmpty(_options.Token)) { queryOptions.Token = _options.Token; } var queryResult = client.Health.Service(name, "", true, queryOptions).ConfigureAwait(false).GetAwaiter() .GetResult(); if (!_manager.Contains(name)) { var token = new ServiceChangeToken(name, queryResult.LastIndex); if (_manager.TryAdd(name, token)) { ThreadPool.QueueUserWorkItem(Watch, token); ChangeToken.OnChange(() => token, InvokeChanged, token.Name); } } var list = new List <ServiceEndPoint>(); foreach (var entry in queryResult.Response) { var endpoint = new ServiceEndPoint { Id = entry.Service.ID, Address = entry.Service.Address, Port = entry.Service.Port, Weight = 1, EffectiveWeight = 1, CurrentWeight = 0 }; list.Add(endpoint); } return(list); }