void BeginUpdateServiceIndexDocument()
        {
            // Get out quick if we don't have anything to do.
            if (_serviceIndexDocumentUpdating || DateTime.UtcNow <= _serviceIndexDocument.UpdateTime + _serviceIndexDocumentExpiration)
            {
                return;
            }

            // Lock to make sure that we can only attempt one update at a time.
            lock (_serviceIndexDocumentLock)
            {
                if (_serviceIndexDocumentUpdating)
                {
                    return;
                }

                _serviceIndexDocumentUpdating = true;
            }

            _httpClient.GetStringAsync(_serviceIndexUri).ContinueWith(t =>
            {
                try
                {
                    JObject serviceIndexDocument = JObject.Parse(t.Result);
                    _serviceIndexDocument        = new ServiceIndexDocument(serviceIndexDocument, DateTime.UtcNow);
                }
                finally
                {
                    _serviceIndexDocumentUpdating = false;
                }
            });
        }
        public async Task DiscoverEndpointsAsync()
        {
            // Get out quick if we don't have anything to do.
            if (_serviceIndexDocument != null && (_serviceIndexDocumentUpdating || DateTime.UtcNow <= _serviceIndexDocument.UpdateTime + _serviceIndexDocumentExpiration))
            {
                return;
            }

            // Lock to make sure that we can only attempt one update at a time.
            lock (_serviceIndexDocumentLock)
            {
                if (_serviceIndexDocumentUpdating)
                    return;

                _serviceIndexDocumentUpdating = true;
            }

            // Fetch the service index document
            await _httpClient.GetStringAsync(_serviceDiscoveryEndpoint)
                .ContinueWith(t =>
                {
                    try
                    {
                        JObject serviceIndexDocument = JObject.Parse(t.Result);
                        _serviceIndexDocument = new ServiceIndexDocument(serviceIndexDocument, DateTime.UtcNow);
                    }
                    finally
                    {
                        _serviceIndexDocumentUpdating = false;
                    }
                }).ConfigureAwait(false);
        }
Exemple #3
0
        public async Task DiscoverEndpointsAsync()
        {
            // Get out quick if we don't have anything to do.
            if (_serviceIndexDocument != null && (_serviceIndexDocumentUpdating || DateTime.UtcNow <= _serviceIndexDocument.UpdateTime + _serviceIndexDocumentExpiration))
            {
                return;
            }

            // Lock to make sure that we can only attempt one update at a time.
            lock (_serviceIndexDocumentLock)
            {
                if (_serviceIndexDocumentUpdating)
                {
                    return;
                }

                _serviceIndexDocumentUpdating = true;
            }

            // Fetch the service index document
            await _httpClient.GetStringAsync(_serviceDiscoveryEndpoint)
            .ContinueWith(t =>
            {
                try
                {
                    JObject serviceIndexDocument = JObject.Parse(t.Result);
                    _serviceIndexDocument        = new ServiceIndexDocument(serviceIndexDocument, DateTime.UtcNow);
                }
                finally
                {
                    _serviceIndexDocumentUpdating = false;
                }
            });
        }
 private ServiceDiscovery(HttpClient httpClient, Uri serviceIndexUri, JObject serviceIndexDocument)
 {
     _httpClient                   = httpClient;
     _serviceIndexUri              = serviceIndexUri;
     _serviceIndexDocument         = new ServiceIndexDocument(serviceIndexDocument, DateTime.UtcNow);
     _serviceIndexDocumentUpdating = false;
     _serviceIndexDocumentLock     = new object();
 }
 private async Task DiscoverEndpointsCoreAsync()
 {
     // Fetch the service index document
     await _httpClient.GetStringAsync(_serviceDiscoveryEndpoint)
     .ContinueWith(t =>
     {
         try
         {
             JObject serviceIndexDocument = JObject.Parse(t.Result);
             _serviceIndexDocument        = new ServiceIndexDocument(serviceIndexDocument, DateTime.UtcNow);
         }
         finally
         {
             _serviceIndexDocumentUpdating = false;
         }
     }).ConfigureAwait(false);
 }
        public async Task <IEnumerable <Uri> > GetEndpointsForResourceType(string resourceType)
        {
            try
            {
                await DiscoverEndpointsAsync().ConfigureAwait(false);
            }
            catch (NullReferenceException)
            {
                // if this happens, we may have received an invalid document
                if (_serviceIndexDocument == null)
                {
                    // if we don't have a cached instance, throw (as we can't do anything here)
                    throw;
                }

                // if we do have a cached one, keep it for another minute
                _serviceIndexDocument = new ServiceIndexDocument(_serviceIndexDocument.Doc, DateTime.UtcNow.AddMinutes(1) - _serviceIndexDocumentExpiration);
            }

            return(_serviceIndexDocument.Doc["resources"]
                   .Where(j => (j["@type"].Type == JTokenType.Array ? j["@type"].Any(v => (string)v == resourceType) : ((string)j["@type"]) == resourceType))
                   .Select(o => o["@id"].ToObject <Uri>())
                   .ToList());
        }
        public async Task<IEnumerable<Uri>> GetEndpointsForResourceType(string resourceType)
        {
            try
            {
                await DiscoverEndpointsAsync().ConfigureAwait(false);
            }
            catch (NullReferenceException)
            {
                // if this happens, we may have received an invalid document
                if (_serviceIndexDocument == null)
                {
                    // if we don't have a cached instance, throw (as we can't do anything here)
                    throw;
                }

                // if we do have a cached one, keep it for another minute
                _serviceIndexDocument = new ServiceIndexDocument(_serviceIndexDocument.Doc, DateTime.UtcNow.AddMinutes(1) - _serviceIndexDocumentExpiration);
            }

            return _serviceIndexDocument.Doc["resources"]
                .Where(j => (j["@type"].Type == JTokenType.Array ? j["@type"].Any(v => (string)v == resourceType) : ((string)j["@type"]) == resourceType))
                .Select(o => o["@id"].ToObject<Uri>())
                .ToList();
        }
 private async Task DiscoverEndpointsCoreAsync()
 {
     // Fetch the service index document
     await _httpClient.GetStringAsync(_serviceDiscoveryEndpoint)
         .ContinueWith(t =>
         {
             try
             {
                 JObject serviceIndexDocument = JObject.Parse(t.Result);
                 _serviceIndexDocument = new ServiceIndexDocument(serviceIndexDocument, DateTime.UtcNow);
             }
             finally
             {
                 _serviceIndexDocumentUpdating = false;
             }
         }).ConfigureAwait(false);
 }