1

我試圖按照ASP.NET Core 1.1下的文章Access Azure Storage in an ASP.NET Core application using Connected Services將我的Web應用程序連接到Azure表。ASP.NET Core中的DI異常:無法創建類型爲「MyProject.Data.ITableRepositories」的實例

但問題是,在ASP.NET Core和Microsoft.WindowsAzure.Storage nuget我沒有acron的Syncronous方法。

我與異步編程初學者,所以大概代碼波紋管是瘋了一點點,但是這是我的版本的「異步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 控制器動作它trows我下面的:

出現InvalidOperationException:無法創建類型「MyProject.Data.ITableRepositories」的一個實例。模型綁定的複雜類型不能是抽象或值類型,並且必須具有無參數的構造函數。

可能我在服務中註冊了錯誤,因爲我的TableClientOperationsService類不是抽象的,並且有一個參數少的構造函數?如何解決它?

+0

我不好讀錯 – Nkosi

回答

3

您應該在控制器的構造函數中獲取ITableRepositories參數,而不是從DI獲取它的方法。

public class HelloWorldController : Controller 
{ 
    private readonly ITableRepositories _tableRepository; 
    public HelloWorldController(ITableRepositories tableRepository) 
    { 
     _tableRepository = tableRepository; 
    } 
} 
+0

你是我的英雄! :)我在'GetRecordsAsync'方法中發現了以下錯誤,因爲EntityResolver爲null,我不知道該放什麼! – Serge

+0

我不熟悉這個EntityResolver的東西( –

+0

謝謝你的幫助! – Serge

相關問題