C# 调用DataServiceQuery.EndExecute()时Windows应用商店应用程序、WCF数据服务InvalidOperationException?
我查找相关数据发现,Windows应用商店应用程序仅支持异步WCF调用,以确保响应的UI 我实现了我的数据服务并这样做。代码如下:C# 调用DataServiceQuery.EndExecute()时Windows应用商店应用程序、WCF数据服务InvalidOperationException?,c#,wcf,windows-8,windows-store-apps,wcf-data-services,C#,Wcf,Windows 8,Windows Store Apps,Wcf Data Services,我查找相关数据发现,Windows应用商店应用程序仅支持异步WCF调用,以确保响应的UI 我实现了我的数据服务并这样做。代码如下: MyDataServiceContext ctx = new MyDataServiceContext(uri); DataServiceQuery<COURSE_OK> query = (DataServiceQuery<COURSE_OK>)(from crs in ctx.COURSE_OK
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);
}
MyDataServiceContext ctx=新的MyDataServiceContext(uri);
数据服务查询=
(DataServiceQuery)(来自ctx.COURSE\u中的crs)确定
选择crs);
TaskFactory tf=新的TaskFactory();
var result=await tf.fromsync(query.BeginExecute(null,null),
ira=>query.EndExecute(ira));//无效操作例外
foreach(结果中的var a)
{
System.Diagnostics.Debug.WriteLine(“{0}”,a.TITLE);
}
我只成功了一次,之后它总是在query.EndExecute(ira)方法中崩溃,并说InvalidOperationException未经用户代码处理
此外,它在控制台应用程序中运行良好。我想主要的问题是Windows应用商店应用程序,但如何解决这个问题?您的功能看起来相当不错。这对我来说很有用:
var queryTask = Task.Factory.FromAsync<IEnumerable<TResult>>(query.BeginExecute(null, null), (asResult) =>
{
var result = query.EndExecute(asResult).ToList();
return result;
});
var queryTask=Task.Factory.fromsync(query.BeginExecute(null,null),(asResult)=>
{
var result=query.EndExecute(asResult.ToList();
返回结果;
});
也许您不应该在每次调用时初始化上下文。只需初始化一次,并在每次需要时重用它