簡體   English   中英

如何在UWP中使用BeginExecute與分頁異步使用WCF數據服務

[英]How to asynchronously consume a WCF Data Service using paging with BeginExecute in UWP

我有WCF數據服務服務,其方法返回Queryable列表。

使用Java庫,我可以在客戶端分頁中使用它(使用toptake )。

現在,我打算通過UWP應用程序使用它。

不幸的是,以下代碼不起作用:

var result = client.CreateQuery<Information>("GetInformation")
             .AddQueryOption("id", string.Format("'{0}'", actualId))
             .Skip(index)
             .Take(amount).ToList();

我得到這個例外:

此目標框架使您無法直接枚舉數據服務查詢。 這是因為枚舉自動將同步請求發送到數據服務。 因為此框架僅支持異步操作,所以必須改為調用BeginExecute和EndExecute方法以獲得支持枚舉的查詢結果。

由於必須使用BeginExecute ,因此不能再使用Skip and Take

var query = client.CreateQuery<Information>("GetInformation")
                  .AddQueryOption("id", string.Format("'{0}'", actualId));
query.BeginExecute((x) =>
          {
            var result= query.EndExecute(x);
            foreach (var information in result)
            {

            };                       
          }, null);

我應該如何使用這種異步方法呢?

我在使用OData時遇到了同樣的問題。 我在下一個博客http://jqyblogger.blogspot.com/2013/11/linq-query-error-message-on-windows.html中找到了解決方案,基本上,我們需要對OData使用asyncronus工具。

            Default.Container context = new Default.Container(new Uri("url_to_OData_Service"));
        var nquery = context.Reports
                               .Where(r => r.ReportId == 1)
                               .Select(p => p);

        DataServiceQuery<Report> query = (DataServiceQuery<Report>)nquery;

        TaskFactory<IEnumerable<Report>> taskFactory = new TaskFactory<IEnumerable<Report>>();
        IEnumerable<Report> result = await taskFactory.FromAsync(query.BeginExecute(null, null), iar => query.EndExecute(iar));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM