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); }