[英]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.