.net 如何创建Singleton以注入Azure存储客户端和容器
我有一个类,它有一个处理Azure存储事务的方法,目前,每次调用它时,我都会创建StorageContainer、BlobClient和Container:.net 如何创建Singleton以注入Azure存储客户端和容器,.net,azure,dependency-injection,singleton,azure-storage,.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(ImageSav
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方法更改为:
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
}
private只读IAzureStorageConfig\u-config;
专用只读IAzureStorage\u存储;
public SaveImageBlob(IAzureStorageConfig配置,
Iazu(恢复存储)
{
_config=config;
_储存=储存;
}
公共异步任务ExecuteAsync(ImageSaveInput)
{
//现在,由于我不必创建storageAccount、blobClient和container的实例,所以我只需从存储中访问它们即可
//创建blockBlob
CloudBlockBlob=\u storage.Container.getblockblobbreference(input.BlobUrl);
//…用流做一些事情(其实并不重要)
//上传blob
blockBlob.UploadFromStreamAsync(流);
//做更多的事情,做更多的事情,回报一些东西
}
我只需要知道如何实现接口,该接口将允许我将AzureStorage类作为一个单例DI到SaveImageBlob中。这并不重要,但为了子孙后代,我将Autofac用于DI。我的项目中也有类似的要求 我解决这个问题的方法是创建一个
StorageTableProviderFactory
…这个工厂只会在启动时创建,但会为DI容器提供所需的引用。工厂将返回一个对象IStorageTableProvider
,现在可以根据需要注入该对象
==============
var StorageTableProviderOptions = new Core.StorageTableProviderProvider.Options()
{
Mode = DataSettings.StorageTableProviderProvider_mode,
StorageTable_ConnectionString = DataSettings.StorageTableProvider_StorageTable_ConnectionString
};
container.Register(() => StorageTableProviderFactory.CreateNew(StorageTableProviderOptions));
单例的基本思想是由一个私有静态实例变量和一个公共静态实例属性组成,该属性通过线程安全地检查实例变量是否已设置,如果未设置则设置,然后返回
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;
}
}
}
留给读者的练习是提供配置
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;
}
}
}