1

WebコアアプリケーションをAzureテーブルに接続するために、ASP.NET Core 1.1の記事Access Azure Storage in an ASP.NET Core application using Connected Servicesに従ってみます。ASP.NET CoreのDI例外: 'MyProject.Data.ITableRepositories'タイプのインスタンスを作成できません

しかし、問題は、ASP.NETコアとMicrosoft.WindowsAzure.Storageナゲットでは、私はSyncronousメソッドへのアクセス権がないことです。

私は非同期プログラミングと初心者ですので、おそらくコード怒鳴るが少し狂っている、しかし、これは「非同期アズールコネクタ」の私のバージョンです。しかし

// 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(); 
    }  

、私がアクセスもしようとすると、

と、InvalidOperationException:型「MyProject.Data.ITableRepositories」のインスタンスを作成できませんでした、それは私に次のことをtrows /HelloWorld/ReadTables1 を経由してコントローラのアクション。モデルにバインドされた複合型は、抽象型または値型であってはならず、パラメータのないコンストラクタも必要です。

おそらく、私のTableClientOperationsServiceクラスが抽象クラスでなく、パラメータが少ないコンストラクタを持っているため、これをサービスで間違って登録しましたか?それを修正するには?

+0

私の悪い読んだ – Nkosi

答えて

3

DIから取得する方法ではなく、コントローラのコンストラクタにITableRepositoriesパラメータを取得する必要があります。

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

あなたは私のヒーローです! :) EntityResolverがnullであるため、 'GetRecordsAsync'メソッドで次のエラーが発生しました。何を置くべきかわかりません! – Serge

+0

私はこのEntityResolverのものに慣れていません( –

+0

あなたの助けてくれてありがとう! – Serge

関連する問題