I have a BlobStorageRepository class which is being used to interact with Azure blob storage. It implements an interface IBlobStorageRepository.
I have the need to use two instances of BlobStorageRepository but they are setup with different credentials:
In my Startup.cs:
// Normal storage
var storageCredentials = new StorageCredentials(_appSettings.BlobStorageAccountName, _appSettings.BlobStorageAccountKey);
var cloudBlobStorageAccount = new CloudStorageAccount(storageCredentials, true);
var cloudBlobClient = cloudBlobStorageAccount.CreateCloudBlobClient();
var blobRepository = new BlobStorageRepository(cloudBlobClient);
// Quarantine storage
var quarantineStorageCredentials = new StorageCredentials(_appSettings.QuarantineBlobStorageAccountName, _appSettings.QuarantineBlobStorageAccountKey);
var quarantineCloudBlobStorageAccount = new CloudStorageAccount(quarantineStorageCredentials, true);
var quarantineCloudBlobClient = quarantineCloudBlobStorageAccount.CreateCloudBlobClient();
var quarantineBlobRepository = new BlobStorageRepository(quarantineCloudBlobClient);
services.AddSingleton<IBlobStorageRepository>(blobRepository);
services.AddSingleton<IBlobStorageRepository>(quarantineBlobRepository);
How can I configure the above so that I can resolve the dependencies in my class and get the two different instances i.e:
private readonly IBlobStorageRepository _blobStorageRepository;
private readonly IBlobStorageRepository _quarantineBlobStorageRepository;
public DocumentService(IBlobStorageRepository blobStorageRepository, IBlobStorageRepository quarantineBlobStorageRepository)
{
_blobStorageRepository = blobStorageRepository;
_quarantineBlobStorageRepository = quarantineBlobStorageRepository;
}
Edit: My question is not a duplicate of How to register multiple implementations of the same interface in Asp.Net Core? because I want to pass the instantiated class when adding the service i.e. services.AddSingleton<IBlobStorageRepository>(blobRepository) and you cannot pass the instantiated class to serviceProvider.GetService<BlobStorageRepository>().
_blobStorageRepository = repoFactory.Get(RepoType.Blob);