title: Azure KeyVault - too many connections from Azure Functions
body: <p>We've got some Azure Functions defined in a class using <code>[FunctionName]</code> attributes from the WebJobs SDK. There are several functions in the class and they all need access to secrets stored in an Azure KeyVault. The problem is that we have many hundreds invocations of the functions a minute, and since each one is making a call to the KeyVault, KeyVault is failing with a message saying something like, "Too many connections. Usually only 10 connections are allowed."</p>
  
  <p><code>@crandycodes</code> (Chris Anderson) on Twitter suggested making the <code>KeyVaultClient</code> static. However, the constructor we're using for the <code>KeyVaultClient</code> requires a delegate function for the constructor, and you can't use a static method as a delegate. So how can we make the <code>KeyVaultClient</code> static? That should allow the functions to share the client, reducing the number of sockets.</p>
  
  <p>Here's our <code>KeyVaultHelper</code> class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class KeyVaultHelper
  {
      public string ClientId { get; protected set; }
  
      public string ClientSecret { get; protected set; }
  
      public string VaultUrl { get; protected set; }
  
      public KeyVaultHelper(string clientId, string secret, string vaultName = null)
      {
          ClientId = clientId;
          ClientSecret = secret;
          VaultUrl = vaultName == null ? null : $"https://{vaultName}.vault.azure.net/";
      }
  
      public async Task&lt;string&gt; GetSecretAsync(string key)
      {
          try
          {
  
              using (var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync),
                  new HttpClient()))
              {
                  var secret = await client.GetSecretAsync(VaultUrl, key);
                  return secret.Value;
              }
          }
          catch (Exception ex)
          {
              throw new ApplicationException($"Could not get value for secret {key}", ex);
          }
      }
  
      public async Task&lt;string&gt; GetAccessTokenAsync(string authority, string resource, string scope)
      {
          var authContext = new AuthenticationContext(authority, TokenCache.DefaultShared);
          var clientCred = new ClientCredential(ClientId, ClientSecret);
          var result = await authContext.AcquireTokenAsync(resource, clientCred);
  
          if (result == null)
          {
              throw new InvalidOperationException("Could not get token for vault");
          }
  
          return result.AccessToken;
      }
  }
  </code></pre>
  
  <p>Here's how we reference the class from our functions:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class ProcessorEntryPoint
  {
      [FunctionName("MyFuncA")]
      public static async Task ProcessA(
          [QueueTrigger("queue-a", Connection = "queues")]ProcessMessage msg,
          TraceWriter log
          )
      {
          var keyVaultHelper = new KeyVaultHelper(CloudConfigurationManager.GetSetting("ClientId"), CloudConfigurationManager.GetSetting("ClientSecret"),
              CloudConfigurationManager.GetSetting("VaultName"));
          var secret = keyVaultHelper.GetSecretAsync("mysecretkey");
          // do a stuff
      }
  
      [FunctionName("MyFuncB")]
      public static async Task ProcessB(
          [QueueTrigger("queue-b", Connection = "queues")]ProcessMessage msg,
          TraceWriter log
          )
      {
          var keyVaultHelper = new KeyVaultHelper(CloudConfigurationManager.GetSetting("ClientId"), CloudConfigurationManager.GetSetting("ClientSecret"),
              CloudConfigurationManager.GetSetting("VaultName"));
          var secret = keyVaultHelper.GetSecretAsync("mysecretkey");
          // do b stuff
      }
  }
  </code></pre>
  
  <p>We could make the <code>KeyVaultHelper</code> class static, but that in turn would need a static <code>KeyVaultClient</code> object to avoid creating a new connection on each function call - so how do we do that or is there another solution? We can't believe that functions that require KeyVault access are not scalable!?</p>