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 Microsoft.Azure.KeyVault 3.0.3
NuGet package and the secrets are accessed using the KeyVaultClient
and .GetSecretAsync()
. All resources are located in the same Azure region.
For the most part this works very well, and for about 90% of the time it returns the secret in milliseconds.
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.
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.
The exception is:
Looking at the telemetry and stepping through the code there is no element of commonality or obvious cause. It seems to be entirely random.
The bottom line is the Azure hosted AppService sometimes cannot connect to an Azure hosted Key Vault in the same datacentre, using the latest framework and SDK version.
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.
EDIT (1): I have now tried spinning up a new Key Vault in a different region entirely, and the problem remains exactly the same.