c# - 如何使用 Azure TableClient 2.0 BeginExecuteQuerySegmented

标签 c# .net asynchronous azure azure-table-storage

我正在尝试异步获取表中的所有条目,但无法弄清楚如何使用 continuation token .我怀疑我需要采用我的匿名方法并将其转换为委托(delegate),然后使用延续 token 递归调用它。

如何获取以下代码并执行异步调用并获取新 API 中的所有条目?

 Task<string[]> GetAllTableEntries(CloudTable tbl, string[] urls, string name, CancellationToken token)
    {
        TableRequestOptions reqOptions = new TableRequestOptions() { };
        OperationContext ctx = new OperationContext() { ClientRequestID = "" };
        object state = null;

        // Register Cancelation Token
        ICancellableAsyncResult result = null;

        TableQuery qry = new TableQuery();
        TableContinuationToken tok = null;

        result = tbl.BeginExecuteQuerySegmented(qry, tok, reqOptions, ctx, (o) =>
        {

            var response = (o.AsyncState as CloudTable).EndExecuteQuerySegmented(o);

            Console.WriteLine("Found " + response.Results.Count + " records");

            // The following code was used in the previous version of the SDK
            //
            //26:                      // add first segment of data
            //27:                      pageData.CompletedList.AddRange(
            //28:                          from wu in response.Results
            //29:                          select new CompletedWorkUnit(wu));
            //30:   
            //31:                      // continue fetching segments to complete page
            //32:                      while (response.HasMoreResults)
            //33:                      {
            //34:                          response = response.GetNext();
            //35:                          pageData.CompletedList.AddRange(
            //36:                              from wu in response.Results
            //37:                              select new CompletedWorkUnit(wu));
            //38:                      }
            //39:   
            //40:                      // set continuation token for next page request
            //41:                      pageData.ContinuationToken = response.ContinuationToken;
            //42:                      evt.Set();

        }, state);

        // Add cancellation token according to guidance from Table Client 2.0 Breaking Changes blog entry
        token.Register((o) => result.Cancel(), state);

最佳答案

请试试这个:

    static void ExecuteQuery()
    {
        TableContinuationToken token = null;
        TableRequestOptions reqOptions = new TableRequestOptions() { };
        OperationContext ctx = new OperationContext() { ClientRequestID = "" };
        long totalEntitiesRetrieved = 0;
        while (true)
        {
            CloudTable table = cloudTableClient.GetTableReference("MyTable");
            TableQuery<TempEntity> query = (new TableQuery<TempEntity>()).Take(100);
            System.Threading.ManualResetEvent evt = new System.Threading.ManualResetEvent(false);
            var result = table.BeginExecuteQuerySegmented<TempEntity>(query, token, reqOptions, ctx, (o) =>
            {
                var response = (o.AsyncState as CloudTable).EndExecuteQuerySegmented<TempEntity>(o);
                token = response.ContinuationToken;
                int recordsRetrieved = response.Count();
                totalEntitiesRetrieved += recordsRetrieved;
                Console.WriteLine("Records retrieved in this attempt = " + recordsRetrieved + " | Total records retrieved = " + totalEntitiesRetrieved);
                evt.Set();
            }, table);
            evt.WaitOne();
            if (token == null)
            {
                break;
            }
        }
    }

我注意到的一件事是,如果我执行返回动态表实体的查询,则会收到与 DateTimeOffset 相关的错误。这就是为什么我最终创建了一个临时实体。

希望这可以帮助。

关于c# - 如何使用 Azure TableClient 2.0 BeginExecuteQuerySegmented,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426430/

相关文章:

c# - Linq Lambda表达式==和Equals运算符

c# - Java 和 C# 中 volatile 语义背后的原因是什么

c# - LinkedList <T>是否没有ConvertAll?

c# - 创建类似于C#异常的Java异常

c# - 将数据从模型传递到自定义验证类

c# - 如何使用SOAP UI传递DTO

asp.net-mvc - 将 CurrentUICulture 传递给 ASP.NET MVC 3.0 中的异步任务

javascript - 从Cordova中的getCurrentPosition返回值

javascript - 异步jQuery getJSON调用后访问变量

c# - 在 C# 中读取用 C 格式化的文件