I've implemented a TimeTriggerC# Azure Function to Run a Store Procedure to an Insert a table that is Encrypted using Column Encryption(Always Encrypted, AzureKey Vault As Provider)
The Blocking area for me is that my Function ran successfully at 1 time and got an error for again many times. So I get the success path as a rare case
The Error I faced is
mscorlib: Exception has been thrown by the target of an invocation. System.Data: Key store providers cannot be set more than once.
I done some investigation on this It occurs in the line
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
With in the function
static void InitializeAzureKeyVaultProvider()
{
string clientId = ConfigurationManager.AppSettings["AuthClientId"];
string clientSecret = ConfigurationManager.AppSettings["AuthClientSecret"];
_clientCredential = new ClientCredential(clientId, clientSecret);
// Direct the provider to the authentication delegate
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(GetToken);
Dictionary<string, SqlColumnEncryptionKeyStoreProvider> providers = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
providers.Add(SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider);
// register the provider with ADO.net
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
}
When I tried to look much deeper into the root cause of the error, it will be like
Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException : Key store providers cannot be set more than once. at System.Data.SqlClient.SqlConnection.RegisterColumnEncryptionKeyStoreProviders(IDictionary`2 customProviders)
As I mentioned above I used Azure Key Vault as my Key Store Provider.I properly registered my Keyvault with AD and added the registered application to my key vault (Access Policies).
Why I'm facing this abnormal behavior, like one-time success and another time failure?
Appreciate your Response,
Jayendran