2014-03-02 12 views
7

Ich habe eine grundlegende Azure-Tabellenspeicherabfrage, die mit dem Windows Azure Storage Client 3.0 funktioniert. Was ist der einfachste Weg, um dies in eine asynchrone Abfrage umzuwandeln? Ist es möglich, das Async-Warte-Muster zu verwenden?Die beste Methode zum Ausführen einer asynchronen Abfrage Azure-Tabellenspeicher

Einfügen und Aktualisieren von Entitäten haben XyzAsync() -Methoden ... Ich muss etwas fehlen. Danke für die Hilfe.

+0

einen Blick auf diese ... Ich habe den Code nicht sah eine Weile, aber ich habe mit Ihrem Problem vor langer Zeit http://stackoverflow.com/a/13438313/328397 – LamonteCristo

Antwort

4

Die neuesten Versionen des SDKs unterstützen jetzt async (nuget).

Sie können Ihre Abfrage mit der ExecuteSegmentedAsync Methode ausführen:

var query = (from s in table.CreateQuery<SampleEntity>() 
      where s.PartitionKey == sampleUID.ToString() select s) 
      .AsTableQuery<SampleEntity>(); 

TableContinuationToken continuationToken = null; 
do 
{ 
    // Execute the query async until there is no more result 
    var queryResult = await query.ExecuteSegmentedAsync(continuationToken); 
    foreach (var entity in queryResult) 
    { 

    } 

    continuationToken = queryResult.ContinuationToken; 
} while (continuationToken != null); 

ich eine Probe dieses Tutorial umgewandelt haben (How to use Table storage from .NET):

  1. eine Tabelle erstellen

    async Task CreateATable() 
    { 
        // Retrieve the storage account from the connection string. 
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
         CloudConfigurationManager.GetSetting("StorageConnectionString")); 
    
        // Create the table client. 
        CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
    
        // Create the table if it doesn't exist. 
        CloudTable table = tableClient.GetTableReference("people"); 
        await table.CreateIfNotExistsAsync(); 
    } 
    
  2. Hinzufügen einer Entität zu einer Tabelle

    public class CustomerEntity : TableEntity 
    { 
        public CustomerEntity(string lastName, string firstName) 
        { 
         this.PartitionKey = lastName; 
         this.RowKey = firstName; 
        } 
    
        public CustomerEntity() { } 
    
        public string Email { get; set; } 
    
        public string PhoneNumber { get; set; } 
    } 
    ... 
    //The script: 
    // Retrieve the storage account from the connection string. 
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString")); 
    
    // Create the table client. 
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
    
    // Create the CloudTable object that represents the "people" table. 
    CloudTable table = tableClient.GetTableReference("people"); 
    
    // Create a new customer entity. 
    CustomerEntity customer1 = new CustomerEntity("Harp", "Walter"); 
    customer1.Email = "[email protected]"; 
    customer1.PhoneNumber = "425-555-0101"; 
    
    // Create the TableOperation object that inserts the customer entity. 
    TableOperation insertOperation = TableOperation.Insert(customer1); 
    
    // Execute the insert operation. 
    await table.ExecuteAsync(insertOperation); 
    
  3. Legen Sie eine Charge von Einheiten

    // Retrieve the storage account from the connection string. 
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString")); 
    
    // Create the table client. 
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 
    
    // Create the CloudTable object that represents the "people" table. 
    CloudTable table = tableClient.GetTableReference("people"); 
    
    // Create the batch operation. 
    TableBatchOperation batchOperation = new TableBatchOperation(); 
    
    // Create a customer entity and add it to the table. 
    CustomerEntity customer1 = new CustomerEntity("Smith", "Jeff"); 
    customer1.Email = "[email protected]"; 
    customer1.PhoneNumber = "425-555-0104"; 
    
    // Create another customer entity and add it to the table. 
    CustomerEntity customer2 = new CustomerEntity("Smith", "Ben"); 
    customer2.Email = "[email protected]"; 
    customer2.PhoneNumber = "425-555-0102"; 
    
    // Add both customer entities to the batch insert operation. 
    batchOperation.Insert(customer1); 
    batchOperation.Insert(customer2); 
    
    // Execute the batch operation. 
    await table.ExecuteBatchAsync(batchOperation); 
    
  4. Und so weiter ...

+0

Diese Frage fragt, wie man eine asynchrone Abfrage macht, dann einen Code einer Abfrage veröffentlichen. Diese Antwort bezieht sich auf eine asynchrone Operation. Das ist irrelevant. –

+0

@ LéonPelletier, Antwort aktualisiert – Thomas

0

Sehen Sie sich die folgenden Blogposts des Azure Storage-Teams Tables Deep Dive an. Der neue Table Service Layer enthält asynchrone Vorgänge wie ExecuteQueryAsync (Gegenstück zur Sync ExecuteQuery-Methode), die mit dem Async-Warte-Muster arbeiten. Lesen Sie auch den Beitrag Storage Library 2.1 Release für weitere Informationen zur IQueryable-Unterstützung unter Verwendung der neuen Table Server-Schicht.

Jason

Verwandte Themen