我们正在开发.NET Core 3.0 Web-API,以将图像上传到Azure blob存储。我碰到了一个样本来达到相同的目的。
下面是Startup.cs中使用Autofac的部分,特别是ContainerBuilder和IComponentContext。
private static void ConfigureStorageAccount(ContainerBuilder builder)
{
AzureTableStorageDebugConnectionString = Configuration["Azure:Storage:ConnectionString"];
builder.Register(c => CreateStorageAccount(AzureTableStorageDebugConnectionString));
}
private static CloudStorageAccount CreateStorageAccount(string connection)
{
if (String.IsNullOrEmpty(connection))
{
throw new Exception("Azure Storage connection string is null!");
}
return CloudStorageAccount.Parse(connection);
}
private static void ConfigureServicesWithRepositories(ContainerBuilder builder)
{
builder.RegisterType<ImageUploadService>().AsImplementedInterfaces().InstancePerLifetimeScope();
}
private static void ConfigureAzureCloudBlobContainers(ContainerBuilder builder)
{
builder.Register(c => c.Resolve<CloudStorageAccount>().CreateCloudBlobClient());
builder.Register(c => GetBlobContainer(c, UploadedImagesCloudBlobContainerName))
.Named<CloudBlobContainer>(UploadedImagesCloudBlobContainerName);
}
private static CloudBlobContainer GetBlobContainer(IComponentContext context, string blobContainerName)
{
var blob = context.Resolve<CloudBlobClient>().GetContainerReference(blobContainerName);
var createdSuccessfully = blob.CreateIfNotExistsAsync().Result;
if (createdSuccessfully)
{
blob.SetPermissionsAsync(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
}
return blob;
}
private static void ConfigureCloudBlobContainersForServices(ContainerBuilder builder)
{
builder.RegisterType<ImageUploadService>()
.WithParameter(
(pi, c) => pi.ParameterType == (typeof(CloudBlobContainer)),
(pi, c) => c.ResolveNamed<CloudBlobContainer>(UploadedImagesCloudBlobContainerName))
.AsImplementedInterfaces();
}
是否有可能完全摆脱Autofac并使用Core 3.0在Startup.cs中实现相同的功能?
据我所知,Autofac用于DI。如果您不想使用它,则可以按照以下方式直接将内容上传到您的存储帐户:
string connString = "the connection string from portal for your storage account, DefaultEndpointsProtocol=https;AccountName=storagetest789;AccountKey=G36m***==;EndpointSuffix=core.windows.net";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference("container_name");
cloudBlobContainer.CreateIfNotExists();
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("blob_name");
cloudBlockBlob.UploadFromFile("file_path");
为了避免重新创建CloudBlobClient
,您可以创建一个工厂类,该类可以直接产生CloudBlobContainer
或CloudBlockBlob
。
并且,您可以使用Microsoft official DI implementation。并在Starup
中注册您的工厂。
//For example, the interface is IStorageFactory, and your implementation is MyStroageFactory
services.AddSingleton<IStorageFactory, MyStroageFactory>();
然后,您可以注入工厂。例如,在控制器中:
public class HomeController : Controller
{
private IStorageFactory _myStorageFactory;
public HomeController(IStorageFactory myStorageFactory)
{
_myStorageFactory = myStorageFactory;
}
public IActionResult Index()
{
//For example, I defined a getCloudBlockBlob method in factory
CloudBlockBlob cloudBlockBlob = _myStorageFactory.getCloudBlockBlob("container_name","blob_name");
cloudBlockBlob.UploadFromFile(....);
return Ok("Uploaded!");
}
}