title: Azure Key Vault secret access intermittently failing to connect with socket exception
body: <p>I have an MVC 5 web application running on .NET 4.7.2 and hosted in an Azure AppService, that uses Azure Key Vault to hold secrets. The project uses the <code>Microsoft.Azure.KeyVault 3.0.3</code> NuGet package and the secrets are accessed using the <code>KeyVaultClient</code> and <code>.GetSecretAsync()</code>. All resources are located in the same Azure region.</p>
  
  <p>For the most part this works very well, and for about 90% of the time it returns the secret in milliseconds.</p>
  
  <p><a href="https://i.stack.imgur.com/k6xCB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k6xCB.png" alt="Working Key Vault Access"></a></p>
  
  <p>But every now and then the call to access the Key Vault fails. This doesn't manifest itself as an exception thrown by the SDK, but the web app hangs. Eventually - and normally in around 1 minute but sometimes longer - the secret is returned and all is fine again. This is because the SDK uses a retry pattern, which will keep trying to get the secret.</p>
  
  <p>Looking at Application Insights for the AppService I can see that the GET request generated by the SDK gets an HTTP 500 response from the Key Vault and a SocketException is thrown, with a result code of ConnectFailure.</p>
  
  <p><a href="https://i.stack.imgur.com/kHFhd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kHFhd.png" alt="Exception"></a></p>
  
  <p>The exception is:</p>
  
  <p><a href="https://i.stack.imgur.com/yYPS6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yYPS6.png" alt="Exception"></a></p>
  
  <p>Looking at the telemetry and stepping through the code there is no element of commonality or obvious cause. It seems to be entirely random.</p>
  
  <p>The bottom line is the Azure hosted AppService <em>sometimes</em> cannot connect to an Azure hosted Key Vault in the same datacentre, using the latest framework and SDK version.</p>
  
  <p>Has anyone else seen this or have any idea? I've searched around and found a few instances of people experiencing the same issue, but nobody has a cause or solution.</p>
  
  <p>EDIT (1): I have now tried spinning up a new Key Vault in a different region entirely, and the problem remains exactly the same.</p>