使用连接服务访问 ASP.NET 核心应用程序中的 Azure 存储
Access Azure Storage in an ASP.NET Core application using Connected Services
我尝试按照 ASP.NET Core 1.1 下的文章 Access Azure Storage in an ASP.NET Core application using Connected Services 将我的 Web 应用程序连接到 Azure 表。
我创造了
public interface ITableRepositories
{
void CreateRecord(Record record);
List<Record> GetRecords();
Record GetRecord(string key, string partitionKey = "record");
}
但问题是,在 ASP.NET Core 和 Microsoft.WindowsAzure.Storage
nuget 下,我无法访问 Syncronous 方法。
因此,例如通过 OperationService class 的代码:
public class TableClientOperationsService : ITableRepositories
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
IConfigurationRoot configs;
public TableClientOperationsService(IConfigurationRoot c) {
this.configs = c;
var connStr = this.configs.GetSection("MicrosoftAzureStorage:[...]");
storageAccount = CloudStorageAccount.Parse(connStr.Value);
tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Book");
table.CreateIfNotExists();
}
我被迫执行连接器的异步变体:
public class TableClientOperationsService: ITableRepositories
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor) {
string connectionString = optionsAccessor.Value.MyProjectTablesConnectionString;
storageAccount = CloudStorageAccount.Parse(connectionString);
tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Record");
// ??? How to use the async method in constructor?
var created = await table.CreateIfNotExistsAsync();
}
}
那么异步方法应该是什么?我应该创建一个 静态构造函数 吗?
PS.
还有CreateRecord "async"操作
来自
public void CreateBook(Book bk)
{
Random rnd = new Random();
bk.BookId = rnd.Next(100);
bk.RowKey = bk.BookId.ToString();
bk.PartitionKey = bk.Publisher;
CloudTable table = tableClient.GetTableReference("Book");
TableOperation insertOperation = TableOperation.Insert(bk);
table.Execute(insertOperation);
}
应该转换成(?)
public void CreateRecord(Record record)
{
// var rowKey = (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d19");
TableOperation insertOperation = TableOperation.Insert(record);
this.RecordTable.ExecuteAsync(insertOperation).Wait();
}
或者说
public Task<TableResult> CreateRecord(Record record)
{
TableOperation insertOperation = TableOperation.Insert(record);
return this.RecordTable.ExecuteAsync(insertOperation);
}
最后我做到了
public async Task<bool> CreateRecord(Record record)
{
TableOperation insertOperation = TableOperation.Insert(record);
await this.RecordTable.ExecuteAsync(insertOperation);
return true;
}
还有
public Record GetRecord(string key, string partitionKey = "record")
{
Record myRecord = null;
TableOperation operation = TableOperation.Retrieve<Record>(partitionKey, key);
// ?
myRecord = recordTable.ExecuteAsync(operation).Result as Record;
}
简单的解决方案是只使用 Wait() 异步方法:
public class TableClientOperationsService: ITableRepositories
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor)
{
string connectionString = optionsAccessor.Value.MyProjectTablesConnectionString;
storageAccount = CloudStorageAccount.Parse(connectionString);
tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Record");
table.CreateIfNotExistsAsync().Wait();
}
}
如果您需要异步方法的结果,请尝试结果:
var created = table.CreateIfNotExistsAsync().Result;
就我个人而言,我不太喜欢具有(太多)功能的构造函数。特别是如果它(相对)可能需要一段时间,比如连接到 StorageAccount。您可以实现一个可以异步的 Initalize()
方法。或者您可以实现一个惰性 属性,它只在第一次使用 属性 时创建 CloudTable
(以及到达那里所需的一切)。这也使您能够在 class.
的生命周期内重复使用 CloudTable
这看起来像这样:
private CloudTable _table;
private CloudTable Table
{
get
{
if (_table == null)
{
var storageAccount = CloudStorageAccount.Parse(connectionString);
var tableClient = storageAccount.CreateCloudTableClient();
_table = tableClient.GetTableReference("Record");
_table.CreateIfNotExistsAsync().Wait();
}
return _table;
}
}
我尝试按照 ASP.NET Core 1.1 下的文章 Access Azure Storage in an ASP.NET Core application using Connected Services 将我的 Web 应用程序连接到 Azure 表。
我创造了
public interface ITableRepositories
{
void CreateRecord(Record record);
List<Record> GetRecords();
Record GetRecord(string key, string partitionKey = "record");
}
但问题是,在 ASP.NET Core 和 Microsoft.WindowsAzure.Storage
nuget 下,我无法访问 Syncronous 方法。
因此,例如通过 OperationService class 的代码:
public class TableClientOperationsService : ITableRepositories
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
IConfigurationRoot configs;
public TableClientOperationsService(IConfigurationRoot c) {
this.configs = c;
var connStr = this.configs.GetSection("MicrosoftAzureStorage:[...]");
storageAccount = CloudStorageAccount.Parse(connStr.Value);
tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Book");
table.CreateIfNotExists();
}
我被迫执行连接器的异步变体:
public class TableClientOperationsService: ITableRepositories
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor) {
string connectionString = optionsAccessor.Value.MyProjectTablesConnectionString;
storageAccount = CloudStorageAccount.Parse(connectionString);
tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Record");
// ??? How to use the async method in constructor?
var created = await table.CreateIfNotExistsAsync();
}
}
那么异步方法应该是什么?我应该创建一个 静态构造函数 吗?
PS.
还有CreateRecord "async"操作
来自
public void CreateBook(Book bk)
{
Random rnd = new Random();
bk.BookId = rnd.Next(100);
bk.RowKey = bk.BookId.ToString();
bk.PartitionKey = bk.Publisher;
CloudTable table = tableClient.GetTableReference("Book");
TableOperation insertOperation = TableOperation.Insert(bk);
table.Execute(insertOperation);
}
应该转换成(?)
public void CreateRecord(Record record)
{
// var rowKey = (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d19");
TableOperation insertOperation = TableOperation.Insert(record);
this.RecordTable.ExecuteAsync(insertOperation).Wait();
}
或者说
public Task<TableResult> CreateRecord(Record record)
{
TableOperation insertOperation = TableOperation.Insert(record);
return this.RecordTable.ExecuteAsync(insertOperation);
}
最后我做到了
public async Task<bool> CreateRecord(Record record)
{
TableOperation insertOperation = TableOperation.Insert(record);
await this.RecordTable.ExecuteAsync(insertOperation);
return true;
}
还有
public Record GetRecord(string key, string partitionKey = "record")
{
Record myRecord = null;
TableOperation operation = TableOperation.Retrieve<Record>(partitionKey, key);
// ?
myRecord = recordTable.ExecuteAsync(operation).Result as Record;
}
简单的解决方案是只使用 Wait() 异步方法:
public class TableClientOperationsService: ITableRepositories
{
CloudStorageAccount storageAccount;
CloudTableClient tableClient;
public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor)
{
string connectionString = optionsAccessor.Value.MyProjectTablesConnectionString;
storageAccount = CloudStorageAccount.Parse(connectionString);
tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("Record");
table.CreateIfNotExistsAsync().Wait();
}
}
如果您需要异步方法的结果,请尝试结果:
var created = table.CreateIfNotExistsAsync().Result;
就我个人而言,我不太喜欢具有(太多)功能的构造函数。特别是如果它(相对)可能需要一段时间,比如连接到 StorageAccount。您可以实现一个可以异步的 Initalize()
方法。或者您可以实现一个惰性 属性,它只在第一次使用 属性 时创建 CloudTable
(以及到达那里所需的一切)。这也使您能够在 class.
CloudTable
这看起来像这样:
private CloudTable _table;
private CloudTable Table
{
get
{
if (_table == null)
{
var storageAccount = CloudStorageAccount.Parse(connectionString);
var tableClient = storageAccount.CreateCloudTableClient();
_table = tableClient.GetTableReference("Record");
_table.CreateIfNotExistsAsync().Wait();
}
return _table;
}
}