diff --git a/src/WebJobs.Extensions.DurableTask/AzureStorageAccountProvider.cs b/src/WebJobs.Extensions.DurableTask/AzureStorageAccountProvider.cs index 1e22e2716..7d42ec413 100644 --- a/src/WebJobs.Extensions.DurableTask/AzureStorageAccountProvider.cs +++ b/src/WebJobs.Extensions.DurableTask/AzureStorageAccountProvider.cs @@ -2,9 +2,11 @@ // Licensed under the MIT License. See LICENSE in the project root for license information. using System; +using System.Collections.Concurrent; using DurableTask.AzureStorage; using Microsoft.Azure.WebJobs.Extensions.DurableTask.Options; using Microsoft.Extensions.Configuration; + #if !FUNCTIONS_V1 using Microsoft.Azure.WebJobs.Extensions.DurableTask.Auth; using Microsoft.WindowsAzure.Storage.Auth; @@ -19,6 +21,9 @@ internal sealed class AzureStorageAccountProvider : IStorageAccountProvider #if !FUNCTIONS_V1 private readonly ITokenCredentialFactory credentialFactory; + private readonly ConcurrentDictionary cachedTokenCredentials = + new ConcurrentDictionary(); + public AzureStorageAccountProvider(IConnectionInfoResolver connectionInfoResolver, ITokenCredentialFactory credentialFactory) { this.connectionInfoResolver = connectionInfoResolver ?? throw new ArgumentNullException(nameof(connectionInfoResolver)); @@ -44,7 +49,9 @@ public StorageAccountDetails GetStorageAccountDetails(string connectionName) AzureStorageAccountOptions account = connectionInfo.Get(); if (account != null) { - TokenCredential credential = this.credentialFactory.Create(connectionInfo); + TokenCredential credential = this.cachedTokenCredentials.GetOrAdd( + connectionName, + attr => this.credentialFactory.Create(connectionInfo)); return new StorageAccountDetails { diff --git a/test/Common/AzureStorageAccountProviderTests.cs b/test/Common/AzureStorageAccountProviderTests.cs index 67774b2ec..886903f37 100644 --- a/test/Common/AzureStorageAccountProviderTests.cs +++ b/test/Common/AzureStorageAccountProviderTests.cs @@ -80,11 +80,11 @@ public void GetStorageAccountDetails_ConfigSection_Endpoints() Assert.Equal(options.TableServiceUri, actual.TableServiceUri); // Get CloudStorageAccount - CloudStorageAccount acount = actual.ToCloudStorageAccount(); - Assert.Same(actual.StorageCredentials, acount.Credentials); - Assert.Equal(options.BlobServiceUri, acount.BlobEndpoint); - Assert.Equal(options.QueueServiceUri, acount.QueueEndpoint); - Assert.Equal(options.TableServiceUri, acount.TableEndpoint); + CloudStorageAccount account = actual.ToCloudStorageAccount(); + Assert.Same(actual.StorageCredentials, account.Credentials); + Assert.Equal(options.BlobServiceUri, account.BlobEndpoint); + Assert.Equal(options.QueueServiceUri, account.QueueEndpoint); + Assert.Equal(options.TableServiceUri, account.TableEndpoint); } [Fact]