我尝试按照文章在核心 1.1 下使用连接的服务访问 ASP.NET 核心应用程序中的 Azure 存储 ASP.NET 以便将我的 Web 应用程序连接到 Azure 表。
但问题是,在 ASP.NET Core 和Microsoft.WindowsAzure.Storage
nuget 下,我无法使用同步方法。
我是异步编程的初学者,所以下面的代码可能有点疯狂,但这是我的"异步 Azure 连接器"版本:
// Model
public class Record : TableEntity {
public Record() { }
public Record(string id, string name, string partition = "record") {
this.RowKey = id;
this.PartitionKey = partition;
this.Name = name;
}
[...]
}
// Data Interface
public interface ITableRepositories {
Task<bool> CreateRecordAsync(Record record);
Task<List<Record>> GetRecordsAsync();
Task<Record> GetRecordAsync(string key, string partitionKey = "record");
}
// Data Class
public class TableClientOperationsService : ITableRepositories {
private string connectionString;
private CloudTable recordTable;
public TableClientOperationsService() { }
public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor) {
connectionString = optionsAccessor.Value.MyProjectTablesConnectionString;
}
public CloudTable RecordTable {
get {
if (recordTable == null) {
recordTable = GetTable("Record");
}
return recordTable;
}
}
private CloudTable GetTable(string tableName) {
[.connectionString.]
table.CreateIfNotExistsAsync().Wait();
return table;
}
public async Task<T> Get<T>(string partitionKey, string rowKey, string tableName) where T : ITableEntity, new()
{ [...] }
public async Task<bool> CreateRecordAsync(Record record) {
TableOperation insertOperation = TableOperation.Insert(record);
await this.RecordTable.ExecuteAsync(insertOperation);
return true;
}
public async Task<Record> GetRecordAsync(string key, string partitionKey = "record") {
const string TableName = "Record";
Record myRecord = await Get<Record>(partitionKey, key, TableName);
return myRecord;
}
public async Task<List<Record>> GetRecordsAsync() {
TableQuery<Record> recordsQuery = new TableQuery<Record>().Where(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "record"));
EntityResolver<Record> recordResolver = null;
List<Record> myList = new List<Record>();
TableQuerySegment<Record> currentSegment = null;
while (currentSegment == null || currentSegment.ContinuationToken != null) {
currentSegment = await recordTable.ExecuteQuerySegmentedAsync(
recordsQuery,
recordResolver,
currentSegment != null ? currentSegment.ContinuationToken : null);
myList.AddRange(currentSegment.Results);
}
return myList;
}
}
// Setup.cs configuration
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSecrets>(Configuration);
// association of ITableRepositories with TableClientOperationsService
services.AddSingleton(typeof(ITableRepositories), typeof(TableClientOperationsService));
services.AddMvc();
}
// Usage in the Controller
public class HelloWorldController : Controller {
public async Task<string> ReadTables1(ITableRepositories repository) {
StringBuilder response = new StringBuilder();
response.AppendLine("Here is your Record Table:");
var items = await repository.GetRecordsAsync();
foreach (Record item in items) {
response.AppendLine($"RowKey: {item.RowKey}; Name: {item.Name}");
}
return response.ToString();
}
但是,当我尝试通过/HelloWorld/ReadTables1
访问控制器操作时 它给我带来了以下问题:
InvalidOperationException:无法创建类型为"MyProject.Data.ITableRepository"的实例。模型绑定的复杂类型不能是抽象类型或值类型,并且必须具有无参数构造函数。
可能是我在服务中错误地注册了它,因为我的 TableClientOperationsService 类不是抽象的,并且有一个参数较少的构造函数?如何解决?
您应该在控制器的构造函数中获取 ITableRepository 参数,而不是从 DI 获取它的方法中获取它。
public class HelloWorldController : Controller
{
private readonly ITableRepositories _tableRepository;
public HelloWorldController(ITableRepositories tableRepository)
{
_tableRepository = tableRepository;
}
}