/// <summary> /// Initializes cloud metadata dynamically from ARM. /// </summary> private static async Task <List <ArmMetadata> > InitializeEnvironmentsFromArm(string armMetadataRequestUri, IHttpOperations httpOperations = null) { if (httpOperations == null) { httpOperations = HttpClientOperationsFactory.Create().GetHttpOperations(); } var armResponseMessage = await httpOperations.GetAsync(armMetadataRequestUri).ConfigureAwait(false); if (armResponseMessage?.StatusCode != HttpStatusCode.OK) { throw new Exception($"Failed to load cloud metadata from the url {armMetadataRequestUri}."); } string armMetadataContent = null; if (armResponseMessage.Content != null) { armMetadataContent = await armResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); } if (string.IsNullOrEmpty(armMetadataContent)) { throw new Exception($"Failed to load cloud metadata from the url {armMetadataRequestUri}."); } return(JsonConvert.DeserializeObject <List <ArmMetadata> >(armMetadataContent)); }
/// <summary> /// Initializes cloud metadata dynamically from ARM. /// </summary> private static async Task <IDictionary <string, AzureEnvironment> > InitializeEnvironmentsFromArm( IHttpOperations httpOperations, string armMetadataRequestUri) { var armResponseMessage = await httpOperations.GetAsync(armMetadataRequestUri); if (armResponseMessage?.StatusCode != HttpStatusCode.OK) { throw new Exception("Failed to load cloud metadata from the url specified by ARM_CLOUD_METADATA_URL."); } var armMetadataContent = await armResponseMessage.Content?.ReadAsStringAsync(); if (string.IsNullOrEmpty(armMetadataContent)) { throw new Exception("Failed to load cloud metadata from the url specified by ARM_CLOUD_METADATA_URL."); } var armMetadataList = JsonConvert.DeserializeObject <List <ArmMetadata> >(armMetadataContent); var result = new ConcurrentDictionary <string, AzureEnvironment>(StringComparer.InvariantCultureIgnoreCase); foreach (var armMetadata in armMetadataList) { result[armMetadata.Name] = MapArmToAzureEnvironment(armMetadataList.First(a => a.Name.Equals(armMetadata.Name, StringComparison.InvariantCultureIgnoreCase))); } SetExtendedProperties(result); return(result); }
void GetOrRenewAuthentication() { if (_expiration - DateTime.UtcNow < TimeSpan.FromMinutes(5)) { var info = _tokenGetter.GetAsync(_requestUri, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); SetToken(info); } }
void GetOrRenewAuthentication() { if (_expiration - DateTime.UtcNow < ManagedServiceTokenInfo.TimeoutThreshold) { ManagedServiceTokenInfo info = null; while (info == null && RequestUris.Count > 0) { var currentRequestUri = RequestUris.Dequeue(); try { info = _tokenGetter.GetAsync(currentRequestUri, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); // if a request was succesful, we should not check any other Uris RequestUris.Clear(); RequestUris.Enqueue(currentRequestUri); } catch (CloudException) when(RequestUris.Count > 0) { // do nothing } } SetToken(info); } }
private void GetOrRenewAuthentication() { if (Expiration - DateTimeOffset.Now < ManagedServiceTokenInfo.TimeoutThreshold) { TManagedServiceTokenInfo info = null; while (info == null && RequestUris.Count > 0) { var currentRequestUri = RequestUris.Dequeue(); try { info = TokenGetter.GetAsync(currentRequestUri, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); // if a request was succesful, we should not check any other Uris RequestUris.Clear(); RequestUris.Enqueue(currentRequestUri); } catch (Exception e) when((e is CloudException || e is HttpRequestException) && RequestUris.Count > 0) { // skip to the next uri } } SetToken(info); } }