简体   繁体   English

Windows应用商店应用程序,调用DataServiceQuery.EndExecute()时WCF数据服务InvalidOperationException?

[英]Windows store app, WCF Data Service InvalidOperationException when calling DataServiceQuery.EndExecute()?

I lookup relative data find that Windows store app only support async WCF calling to ensure the responsive UI. 我查找了相关数据,发现Windows应用商店应用仅支持异步WCF调用以确保响应式UI。 Here is a relative question. 这是一个相对的问题。

I achieve my data service and do like that. 我实现了数据服务并做到了这一点。 Here is the code: 这是代码:

MyDataServiceContext ctx = new MyDataServiceContext(uri);

DataServiceQuery<COURSE_OK> query =
        (DataServiceQuery<COURSE_OK>)(from crs in ctx.COURSE_OK
                                      select crs);

TaskFactory<IEnumerable<COURSE_OK>> tf = new TaskFactory<IEnumerable<COURSE_OK>>();
var result = await tf.FromAsync(query.BeginExecute(null, null),
                                ira => query.EndExecute(ira)); // InvalidOperationException

foreach (var a in result)
{
    System.Diagnostics.Debug.WriteLine("{0}", a.TITLE);
}

I succeeded only once, after that it always crash in query.EndExecute(ira) method and said InvalidOperationException was unhandled by user code. 我只成功了一次,之后它总是在query.EndExecute(ira)方法中崩溃,并说InvalidOperationException未由用户代码处理。

In addition, it works well in Console Application. 另外,它在控制台应用程序中也能很好地工作。 I guess that the main problem is Windows store app, but how to solve this? 我想主要问题是Windows应用商店应用,但是如何解决呢?

Your function looks quite good. 您的功能看起来不错。 This is what works for me: 这对我有效:

var queryTask = Task.Factory.FromAsync<IEnumerable<TResult>>(query.BeginExecute(null, null), (asResult) =>
{
   var result = query.EndExecute(asResult).ToList();
   return result;
});

Maybe you should not initialize your context on every call. 也许您不应该在每次调用时都初始化您的上下文。 Just initialize it once and reuse it every time you need it. 只需将其初始化一次,即可在需要时重新使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用WCF服务时出现System.InvalidOperationException - System.InvalidOperationException when calling WCF Service 从Windows Store应用程序使用Windows Auth调用WCF服务 - Calling a WCF service using Windows Auth from a Windows Store App Windows 8 Store App和WCF服务 - Windows 8 Store App and WCF service 在调用带有多个参数的WCF RESTful服务方法时,是否出现InvalidOperationException? - InvalidOperationException when calling a WCF RESTful service method with multiple arguments? 带有嵌套操作的MVC,带有EF5的WCF数据服务和DataServiceQuery &lt;&gt;。Expand() - MVC with nested actions, WCF Data Service with EF5, and DataServiceQuery<>.Expand() Windows 8应用商店应用程序,无法使用WCF数据服务 - Windows 8 Store App, Can't Use WCF Data Service 通过Windows 8应用程序调用WCF服务时解决连接问题 - Resolve connection issue when calling a WCF service through windows 8 app 在Windows 8移动应用程序上调用WCF服务功能 - Calling WCF service functions on a windows 8 mobile app 调用Asmx Web服务时出现InvalidOperationException - InvalidOperationException when calling asmx web service 从Windows服务调用WCF服务时出现证书错误 - Certificate error when calling WCF service from Windows Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM