public override async Task RunAsync(CancellationToken cancellationToken) { var keyVaultUri = GetEnvironmentVariable("KEYVAULT_URL"); var client = new MiniSecretClient(new Uri(keyVaultUri), new DefaultAzureCredential()); while (!cancellationToken.IsCancellationRequested) { try { // Throttle requests to avoid exceeding service limits await Task.Delay(TimeSpan.FromMilliseconds(Options.Delay), cancellationToken); var secret = await client.GetSecretAsync(Options.SecretName, cancellationToken); Interlocked.Increment(ref Metrics.SecretsReceived); if (secret.Value.Value == "TestValue") { Interlocked.Increment(ref Metrics.CorrectValues); } else { Interlocked.Increment(ref Metrics.IncorrectValues); } } catch (Exception e) when(ContainsOperationCanceledException(e)) { } catch (Exception e) { Metrics.Exceptions.Enqueue(e); } } }
public async Task GettingASecretAsync() { var endpoint = TestEnvironment.KeyVaultUri; #region Snippet:GetSecretAsync #if SNIPPET string endpoint = "https://myvault.vault.azure.net"; #endif var client = new MiniSecretClient(new Uri(endpoint), new DefaultAzureCredential()); SecretBundle secret = await client.GetSecretAsync("TestSecret"); Console.WriteLine(secret.Value); #endregion Assert.NotNull(secret.Value); }