Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Dependency Injection for Two Storage account in an Azure Application

I have an application which i would like to make reference to two different storage accounts (SA1 and SA2) which both has a single contaner each "container1" and "container2" respectively. I have the below DI setup for both (Not sure this is correct)

        serviceCollection.AddAzureClients(builder =>
    {
        builder.AddBlobServiceClient("SA1_connectionStr"))
            .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);

       builder.AddBlobServiceClient("SA2_connectionStr"))
            .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);

    });

When i use the "blobContainer" reference (using the code below) it's only refering the container in the "SA2" storgaeAccount (maybe bcause it was added last in the DI setup)

 private readonly BlobServiceClient blobServiceClient;
 var blobContainer = blobServiceClient.GetBlobContainerClient(container2);

How do i make reference to storageAccount SA1 and storageAccount SA2 seperately?

like image 570
O'Neil Tomlinson Avatar asked Oct 11 '25 12:10

O'Neil Tomlinson


2 Answers

You're going to want to the name the clients in order to differentiate them:

   serviceCollection.AddAzureClients(builder =>
    {
        builder.AddBlobServiceClient("SA1_connectionStr"))
            .WithName("SA1")
            .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);

       builder.AddBlobServiceClient("SA2_connectionStr"))
            .WithName("SA2")
            .WithVersion(BlobClientOptions.ServiceVersion.V2019_02_02);

    });

and then you can access them by injecting:

IAzureClientFactory<BlobServiceClient> clientFactory

into your method's constructor.

Once that's done, you can access each of the clients like this:

_sa1Client = clientFactory.CreateClient("SA1");
_sa2Client = clientFactory.CreateClient("SA2");

and then use those clients to actually interact with the different containers.

You can checkout the official documentation for this here.

like image 125
akseli Avatar answered Oct 14 '25 00:10

akseli


alternitive to akseli

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddBlobServiceClient("SA1", options =>
            {
                options.ConnectionString = "your_connection_string_here_1";
            });

            services.AddBlobServiceClient("SA2", options =>
            {
                options.ConnectionString = "your_connection_string_here_2";
            });

            // Other service registrations...
        }

        // Other methods...
    }
public class YourService
    {
        private readonly BlobServiceClient _blobClientSA1;
        private readonly BlobServiceClient _blobClientSA2;

        public YourService(IServiceProvider serviceProvider)
        {
            // Inject the specific instance you want
            _blobClientSA1 = serviceProvider.GetRequiredService<BlobServiceClient>("SA1");
                _blobClientSA2 = serviceProvider.GetRequiredService<BlobServiceClient>("SA2");
        }

        // Use _blobClient as needed...
    }
like image 32
Seabizkit Avatar answered Oct 14 '25 02:10

Seabizkit