如何创建单例到依赖项注入Azure存储客户端和容器

时间:2017-07-12 21:21:18

标签: .net azure dependency-injection singleton azure-storage

我有一个类有一个Azure存储方法的类,目前,我每次调用时都会创建StorageContainer,BlobClient和Container:

private readonly IAzureStorageConfig _config;

public SaveImageBlob(IAzureStorageConfig config)
{
    _config = config;
}

public async Task<T> ExecuteAsync(ImageSaveInput input)
{

    //get the storage account from the connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_config.ConnectionString);

    //instantiate the client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    //set the container
    CloudBlobContainer container = blobClient.GetContainerReference(_config.ImagesContainerName);

    //... do things and stuff
}

现在,我想从SaveImageBlob.ExecuteAsync方法中删除它,并将依赖项注入到类中,因此Azure存储项只被实例化一次。

我目前有这样的界面:

namespace Domain.Interfaces
{
    public interface IAzureStorage
    {
        CloudStorageAccount StorageAccount { get; }

        CloudBlobClient BlobClient { get; }

        CloudBlobContainer Container { get; }
    }
}

现在,我对如何实现该接口毫无头绪。

实现接口后,我想我要将S​​aveImageBlob.ExecuteAsync方法更改为:

private readonly IAzureStorageConfig _config;
private readonly IAzureStorage _storage;

public SaveImageBlob(IAzureStorageConfig config,
                     IAzureStorage storage)
{
    _config = config;
    _storage = storage;
}

public async Task<T> ExecuteAsync(ImageSaveInput input)
{

    //now, since I don't have to create an instance of the storageAccount, blobClient, and container, I can just access them from _storage
    //create the blockBlob
    CloudBlockBlob blockBlob = _storage.Container.GetBlockBlobReference(input.BlobUrl); 

    //... do things and stuff with a Stream (doesn't really matter)

    //upload the blob
    blockBlob.UploadFromStreamAsync(stream);

    //do more things and stuff and return something 
}

我只需要知道如何实现这个接口,这个接口允许我将AzureStorage类作为Singleton转换为SaveImageBlob。并不重要,但为了后人的缘故,我正在使用Autofac进行DI。

2 个答案:

答案 0 :(得分:4)

单例的基本思想包括私有静态实例变量和公共静态实例属性,该属性可以线程安全地检查是否设置了实例变量,设置它如果不是,则返回。

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;

public sealed class AzureStorage
{
    private static volatile AzureStorage instance;
    private static object syncRoot = new Object();

    public CloudStorageAccount StorageAccount { get; private set; }
    public CloudBlobClient BlobClient { get; private set; }
    public CloudBlobContainer Container { get; private set; }

    private AzureStorage()
    {
        StorageAccount = CloudStorageAccount.Parse(_config.ConnectionString);

        //instantiate the client
        BlobClient = StorageAccount.CreateCloudBlobClient();

        //set the container
        Container = BlobClient.GetContainerReference(_config.ImagesContainerName);

    }

    public static AzureStorage Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new AzureStorage();
                }
            }

            return instance;
        }
    }
}

留给读者的练习是提供配置。

答案 1 :(得分:2)

我的项目有类似的要求。

我解决这个问题的方法是创建一个StorageTableProviderFactory ...这个工厂只会在启动时创建,但会为DI容器提供所需的引用。工厂将返回一个对象IStorageTableProvider,现在可以根据需要进行注入。

==============

        var StorageTableProviderOptions = new Core.StorageTableProviderProvider.Options()
        {
            Mode = DataSettings.StorageTableProviderProvider_mode,
            StorageTable_ConnectionString = DataSettings.StorageTableProvider_StorageTable_ConnectionString
        };


        container.Register(() => StorageTableProviderFactory.CreateNew(StorageTableProviderOptions));