我正在使用 Azure 函数来存储加密数据。我已经使用下面的代码成功地做到了;但是,尝试多次初始化密钥存储提供程序会引发错误,Key store providers cannot be set more than once. 为了避免这种情况,我设置了一个静态变量来跟踪它是否已经初始化。这在短时间内很有效。但是,每次我第二天第一次尝试时,函数都会出现同样的错误。对我来说有趣的是,如果isInitialized静态变量在一段时间后失去了它的值,那么该_clientCredential变量不应该也被重置,这意味着再次尝试设置提供者是可以的吗?
private static ClientCredential _clientCredential;
private static Boolean isInitialized;
if (isInitialized == false) {
isInitialized = true;
string clientId = ConfigurationManager.ConnectionStrings["blah"].ConnectionString;
string clientSecret = ConfigurationManager.ConnectionStrings["blah"].ConnectionString;
_clientCredential = new ClientCredential(clientId, clientSecret);
SqlColumnEncryptionAzureKeyVaultProvider azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(GetToken);
Dictionary<string, SqlColumnEncryptionKeyStoreProvider> providers = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>();
providers.Add(SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider);
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
}
似乎其中一个变量正在被重置,而不是另一个。我了解使用 Azure Functions 也存在线程安全问题。是否有其他方法可以检查它是否仍然完全初始化以完全避免线程安全问题?或者我是否需要自己避免线程安全问题和会话状态?
我看过另一篇关于这个主题的帖子,但没有关于如何处理上述问题的真正解释。