Geeks With Blogs
Rohit Gupta Engaging talk on Microsoft Technologies ....My Resume

When using the storage client library to access Azure Storage, it is recommended that we make use of the Async calls to execute queries against the Azure Tables/Blobs.
TableClient and BlobClient expose methods to execute queries asynchronously, for e.g.

blobClient.BeginListContainersSegmented : which retrieves the list of Blob Containers in our storage.
cloudTableClient.BeginListTablesSegmented() : which retrieves the list of tables in Azure strorage
and many more


Then there is CloudTableQuery class which exposes the BeginExecuteSegmented method which enables us to execute queries asynchronously.

The following example shows how to get the number of rows in a Azure Table.

   1: var context = new TableServiceContext(this.storageAccount.TableEndpoint.ToString(), this.storageAccount.Credentials);
   2: var query = context.CreateQuery<T>(this.tableName).AsTableServiceQuery();
   3:  
   4: if (condition != null)
   5:     query = Query.Where(condition).AsTableServiceQuery();
   6:  
   7: var results = new List<T>();
   8: if (query != null)
   9: {
  10:     //Using Storage Client Library Async API to execute query
  11:     using (var mre = new System.Threading.ManualResetEvent(false))
  12:     {
  13:         query.BeginExecuteSegmented((result) =>
  14:                                         {
  15:                                             var resultSegment =
  16:                                                 query.EndExecuteSegmented(result);
  17:                                             results.AddRange(resultSegment.Results);
  18:                                             while (resultSegment.HasMoreResults)
  19:                                             {
  20:                                                 resultSegment = resultSegment.GetNext();
  21:                                                 results.AddRange(resultSegment.Results);
  22:                                             }
  23:                                             mre.Set();
  24:                                         }, null);
  25:         mre.WaitOne();
  26:     }
  27: }
  28: return results.Count;

Note that currently with version 1.5 of the Azure SDK, there are some instances when the AsTableServiceQuery() extension method throws an exception complaining that the query parameter passed to the CloudTableQuery<T> constructor cannot be null.
This happens when the query is returning the entire table with no filter conditions.
At these times the cast to DataServiceQuery<T> fails and AsTableServiceQuery() throws an exception.
To get around this limitation cast the IQueryable<T> to CloudTableQuery<T> manually instead of calling the AsTableServiceQuery() extension.

   1: var context = new TableServiceContext(this.account.TableEndpoint.ToString(), this.account.Credentials);
   2: var query1 = context.CreateQuery<T>(this.tableName).AsTableServiceQuery();
   3: // if we call AsTableServiceQuery() again on query1 the "Variable cannot be null" exception is thrown
   4: var query = query1 as CloudTableQuery<AzureDummy>;
   5: query.BeginExecuteSegmented();

Here is an excellent post about using Asunc API for working with blobs : Accessing Blobs using Async API.

There some excellent posts by Neil Mackenzie on async API as well (which shows how to use BeginExecute() on DataServiceQuery<T> ): Queries in Azure Tables

Posted on Tuesday, October 18, 2011 2:59 AM | Back to top


Comments on this post: Azure Storage Client using Async API

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Rohit Gupta | Powered by: GeeksWithBlogs.net