20

新しい技術を学ぶときはいつでも、できるだけ単純な例を書くのが好きです。通常、これは参照の数が最も少ないコンソール アプリを意味します。Azure テーブル ストレージに対して読み取りと書き込みを行うアプリを作成しようとしましたが、ほとんど成功しませんでした。このハウツー ガイドをベースとして使用しましたが、Main メソッドですべてを実行してみてください。同様のアプローチはブロブ ストレージでもうまく機能しましたが、テーブル ストレージでは問題が発生しています。

このコードでテーブルを作成できました。

static void Main(string[] args)
{
    Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient =
    new Microsoft.WindowsAzure.Storage.Table.CloudTableClient(
        new Uri("http://mystorage.table.core.windows.net/"),
    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("[somename]", "[somekey]"));

    CloudTable table = tableClient.GetTableReference("people");
    table.CreateIfNotExists();
}

このコードを実行した後、 Azure Storage Explorerを使用してストレージ内のテーブルを確認できました。(manage.windowsazure.com でテーブルを表示する方法はまだわかりません。)

ただし、レコードを挿入しようとすると (前述のハウツー ガイドで説明されているように)、競合 409 EntityAlreadyExists が発生します。Azure Storage Explorer のテーブルにレコードが表示されません。

CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter@contoso.com";
customer1.PhoneNumber = "425-555-0101";

TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);

また、2 つの名前空間が重複していることにも困惑しています。Microsoft.WindowsAzure.Storage.Table と Microsoft.WindowsAzure.StorageClient の両方に、たとえば CloudTableClient クラスが含まれています。クライアント名前空間が 2 つあるのはなぜですか? どちらを使用すればよいですか?

編集レコードが存在することが判明しました。Azure テーブル エクスプローラーでテーブルをダブルクリックするだけでは、テーブルの内容は表示されません。[クエリ] をクリックする必要があります。最後の質問はまだ残っています。なぜ 2 つの名前空間があるのですか?

4

2 に答える 2

24

私が考えることができる最も単純なサンプルはこれです。NuGet WindowsAzure.Storage 2.0 が必要です。

static void Main(string[] args)
{
  try
  {
     CloudStorageAccount storageAccount =
        CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<your_storage_name>;AccountKey=<your_account_key>");
     CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

     CloudTable table = tableClient.GetTableReference("people");
     table.CreateIfNotExists();

     CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
     customer1.Email = "Walter@contoso.com";
     customer1.PhoneNumber = "425-555-0101";

     // Create the TableOperation that inserts the customer entity.
     var insertOperation = TableOperation.Insert(customer1);

     // Execute the insert operation.
     table.Execute(insertOperation);

     // Read storage
     TableQuery<CustomerEntity> query =
        new TableQuery<CustomerEntity>()
           .Where(TableQuery.GenerateFilterCondition("PartitionKey",
               QueryComparisons.Equal, "Harp"));
     var list = table.ExecuteQuery(query).ToList();
   }
   catch (StorageException ex)
   {
       // Exception handling here.
   }
}

public class CustomerEntity : TableEntity
{
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public CustomerEntity(string lastName, string firstName)
    {
        PartitionKey = lastName;
        RowKey = firstName;
    }

    public CustomerEntity() { }
}

2 番目の質問に対する答えとして、ほぼ同じ API を提供する 2 つの名前空間があるのはなぜですか。Azure ストレージ クライアント ライブラリ 2.0 には、新しい簡素化された API が含まれています。以下のリンクを参照してください。

.NET 用ストレージ クライアント ライブラリ (バージョン 2.0) の新機能

于 2013-06-13T13:18:32.243 に答える