我有一个由服务客户端调用的 WCF 服务。我想使用 async / await 构造来包装对此的调用;但是,服务和服务客户端是.NET3.5。我对此的解决方案如下:
private async Task<ObservableCollection<MyEntity>> LoadData(ParamData param)
{
ServiceClient svc = new ServiceClient();
int results = 0;
// Set-up parameters
myParams = BuildParams(param);
// Call a count function to see how much data we're talking about
// This call should be relatively quick
var counter = Task.Factory.StartNew(() =>
{
results = svc.GetResultCount(myParams);
}).ContinueWith((task) =>
{
if (results <= 10000 ||
(MessageBox.Show("More than 10000 results, still retrieve data?"), MessageBoxButton.YesNo) == MessageBoxResult .Yes))
{
return svc.Search(myParams);
}
});
}
我得到编译错误:
Since 'System.Action<System.Threading.Tasks.Task>' returns void, a return keyword must not be followed by an object expression
所以,我的问题是,是否可以以这种方式运行同步方法,如果可以,我做错了什么?我的目标是可以像这样调用该方法:
var data = await LoadData(params);