From 42a3b92a1391544c89739a04837a7e30d7f40111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Robinet?= Date: Fri, 24 May 2024 16:51:42 -0300 Subject: [PATCH] Ensure thread safety for CertificateServiceClientCredentialsFactory --- .../CertificateServiceClientCredentialsFactory.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libraries/Microsoft.Bot.Connector/Authentication/CertificateServiceClientCredentialsFactory.cs b/libraries/Microsoft.Bot.Connector/Authentication/CertificateServiceClientCredentialsFactory.cs index 3beac35d4f..b8defbc1dc 100644 --- a/libraries/Microsoft.Bot.Connector/Authentication/CertificateServiceClientCredentialsFactory.cs +++ b/libraries/Microsoft.Bot.Connector/Authentication/CertificateServiceClientCredentialsFactory.cs @@ -81,10 +81,9 @@ public override Task CreateCredentialsAsync( } // Instance must be reused per audience, otherwise it will cause throttling on AAD. - _certificateAppCredentialsByAudience.TryGetValue(audience, out var certificateAppCredentials); - if (certificateAppCredentials == null) + var certificateAppCredentials = _certificateAppCredentialsByAudience.GetOrAdd(audience, (audience) => { - certificateAppCredentials = new CertificateAppCredentials( + return new CertificateAppCredentials( _certificate, _appId, _tenantId, @@ -92,8 +91,7 @@ public override Task CreateCredentialsAsync( _sendX5c, _httpClient, _logger); - _certificateAppCredentialsByAudience.TryAdd(audience, certificateAppCredentials); - } + }); return Task.FromResult(certificateAppCredentials); }