运行 Azure 存储模拟器以编程方式
Running Azure Storage Emulator programmatically
我正在使用 Azure Blob 存储来存储照片。它工作正常。为了连接到存储,我在 appsettings.json
:
中添加了一个 AzureStorageConfig
"AzureStorageConfig": {
"AccountName": "<accountname>",
"ImageContainer": "<containername>",
"AccountKey": "<accountkey>"
}
我另外创建了一个classAzureStorageConfig
public class AzureStorageConfig
{
public string AccountKey { get; set; }
public string AccountName { get; set; }
public string BaseUrl { get; set; }
public Uri BlobEndpoint { get; set; }
public string ImageContainer { get; set; }
public Uri QueueEndpoint { get; set; }
public Uri TableEndpoint { get; set; }
}
并在 Startup.cs 中配置它:
services.Configure<AzureStorageConfig>(Configuration.GetSection(nameof(AzureStorageConfig)));
因此可以通过依赖注入来注入配置。
对于 appsettings.development.json
,我想使用 Azure 存储模拟器。我找到了几个教程,但它们都使用连接字符串连接到模拟器而不是配置。
我尝试使用在 Microsoft 页面上找到的数据:
"AzureStorageConfig": {
"AccountName": "devstoreaccount1",
"ImageContainer": "images",
"AccountKey": "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
"BlobEndpoint": "http://127.0.0.1:10000/devstoreaccount1",
"TableEndpoint": "http://127.0.0.1:10002/devstoreaccount1",
"QueueEndpoint": "http://127.0.0.1:10001/devstoreaccount1"
}
我这样初始化对象:
public class AzureStorageService
{
private readonly CloudBlobContainer _imageContainer;
private readonly AzureStorageConfig _storageConfig;
public AzureStorageService(IOptions<AzureStorageConfig> config)
{
_storageConfig = config.Value;
CloudStorageAccount storageAccount;
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);
if (_storageConfig.BlobEndpoint == null)
{
storageAccount = new CloudStorageAccount(storageCredentials, true);
}
else
{
storageAccount = new CloudStorageAccount(
storageCredentials,
_storageConfig.BlobEndpoint,
_storageConfig.QueueEndpoint,
_storageConfig.TableEndpoint,
null);
}
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
_imageContainer = blobClient.GetContainerReference(_storageConfig.ImageContainer);
_imageContainer.CreateIfNotExistsAsync().Wait();
}
}
我必须通过启动 "Microsoft Azure Compute Emulator" 应用程序来手动启动模拟器。我如何以编程方式启动(和初始化)模拟器以进行自动化测试(以及运行这些测试的 Azure Devops CI)?
非常感谢。
看来你提供的访问asp.net核心中的appsetting.json文件的代码是没有问题的。你可以参考这个article.
并且当你上传你的blob时,如果你总是遇到这个问题,这意味着机器存在但是它没有服务监听指定的端口,或者有防火墙阻止你。
如果偶尔发生 - 您使用了 "sometimes" 这个词 - 并且重试成功,可能是因为服务器已满 'backlog'.
您应该将您的代码更改为使用连接字符串,这与使用帐户名和密钥相同,但是当您使用模拟器时,只需将连接字符串更改为 "UseDevelopmentStorage=true;"
。
关于启动模拟器,您可以在startup.cs文件中检查环境变量:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
//Start the Emulator here by initiating a new process that calls the emulator.exe file
}
}
另一种解决方案是使用托管服务并对环境进行相同的检查并在 StartAsync
中启动模拟器并在 StopAsync
中停止它
查看此 link 了解更多详细信息 ASP.NET Core 中托管服务的后台任务
我正在使用 Azure Blob 存储来存储照片。它工作正常。为了连接到存储,我在 appsettings.json
:
"AzureStorageConfig": {
"AccountName": "<accountname>",
"ImageContainer": "<containername>",
"AccountKey": "<accountkey>"
}
我另外创建了一个classAzureStorageConfig
public class AzureStorageConfig
{
public string AccountKey { get; set; }
public string AccountName { get; set; }
public string BaseUrl { get; set; }
public Uri BlobEndpoint { get; set; }
public string ImageContainer { get; set; }
public Uri QueueEndpoint { get; set; }
public Uri TableEndpoint { get; set; }
}
并在 Startup.cs 中配置它:
services.Configure<AzureStorageConfig>(Configuration.GetSection(nameof(AzureStorageConfig)));
因此可以通过依赖注入来注入配置。
对于 appsettings.development.json
,我想使用 Azure 存储模拟器。我找到了几个教程,但它们都使用连接字符串连接到模拟器而不是配置。
我尝试使用在 Microsoft 页面上找到的数据:
"AzureStorageConfig": {
"AccountName": "devstoreaccount1",
"ImageContainer": "images",
"AccountKey": "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
"BlobEndpoint": "http://127.0.0.1:10000/devstoreaccount1",
"TableEndpoint": "http://127.0.0.1:10002/devstoreaccount1",
"QueueEndpoint": "http://127.0.0.1:10001/devstoreaccount1"
}
我这样初始化对象:
public class AzureStorageService
{
private readonly CloudBlobContainer _imageContainer;
private readonly AzureStorageConfig _storageConfig;
public AzureStorageService(IOptions<AzureStorageConfig> config)
{
_storageConfig = config.Value;
CloudStorageAccount storageAccount;
StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey);
if (_storageConfig.BlobEndpoint == null)
{
storageAccount = new CloudStorageAccount(storageCredentials, true);
}
else
{
storageAccount = new CloudStorageAccount(
storageCredentials,
_storageConfig.BlobEndpoint,
_storageConfig.QueueEndpoint,
_storageConfig.TableEndpoint,
null);
}
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
_imageContainer = blobClient.GetContainerReference(_storageConfig.ImageContainer);
_imageContainer.CreateIfNotExistsAsync().Wait();
}
}
我必须通过启动 "Microsoft Azure Compute Emulator" 应用程序来手动启动模拟器。我如何以编程方式启动(和初始化)模拟器以进行自动化测试(以及运行这些测试的 Azure Devops CI)?
非常感谢。
看来你提供的访问asp.net核心中的appsetting.json文件的代码是没有问题的。你可以参考这个article.
并且当你上传你的blob时,如果你总是遇到这个问题,这意味着机器存在但是它没有服务监听指定的端口,或者有防火墙阻止你。
如果偶尔发生 - 您使用了 "sometimes" 这个词 - 并且重试成功,可能是因为服务器已满 'backlog'.
您应该将您的代码更改为使用连接字符串,这与使用帐户名和密钥相同,但是当您使用模拟器时,只需将连接字符串更改为 "UseDevelopmentStorage=true;"
。
关于启动模拟器,您可以在startup.cs文件中检查环境变量:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
//Start the Emulator here by initiating a new process that calls the emulator.exe file
}
}
另一种解决方案是使用托管服务并对环境进行相同的检查并在 StartAsync
中启动模拟器并在 StopAsync
查看此 link 了解更多详细信息 ASP.NET Core 中托管服务的后台任务