写真の保存にAzureBlobStorageを使用しています。それはうまくいきます。ストレージに接続するために、次の場所にAzureStorageConfigを追加しましたappsettings.json
。
"AzureStorageConfig": {
"AccountName": "<accountname>",
"ImageContainer": "<containername>",
"AccountKey": "<accountkey>"
}
さらにクラスを作成しました AzureStorageConfig
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();
}
}
「MicrosoftAzureCompute Emulator」アプリを起動して、エミュレーターを手動で起動する必要がありました。自動テスト(およびこれらのテストを実行するAzure Devops CI)用にエミュレーターをプログラムで起動(および初期化)するにはどうすればよいですか?
どうもありがとう。
アカウント名とキーを使用するのと同じ接続文字列を使用するようにコードを変更する必要がありますが、接続文字列をに変更するだけでエミュレーターを使用する方が簡単です"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
}
}
もう1つの解決策は、ホストされたサービスを使用し、同じ環境チェックを実行して、でエミュレーターを起動し、でエミュレーターStartAsync
を停止することです。StopAsync
詳細については、このリンクを参照してくださいASP.NETCoreでホストされているサービスのバックグラウンドタスク
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加