繁体   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