问题描述
我有一个WCF服务,由服务客户端调用。我想使用异步/等待结构来包装调用这一点;然而,服务和服务客户端是.NET3.5。我此解决方案如下:
专用异步任务<&的ObservableCollection LT; myEntity所>> LoadData(ParamData参数)
{
ServiceClient SVC =新ServiceClient();
INT结果= 0; //的参数设置
myParams = BuildParams(参数); //调用计数功能,看我们有多少数据说起
//此调用应该会比较快
VAR计数器= Task.Factory.StartNew(()=>
{
结果= svc.GetResultCount(myParams);
})ContinueWith((任务)=过夜。;
{
如果(结果< = 10000 ||
(MessageBox.Show(超过10000项,还检索数据?),MessageBoxButton.YesNo)== MessageBoxResult。没错))
{
返回svc.Search(myParams);
}
});
}
我得到的编译错误:
由于'System.Action< System.Threading.Tasks.Task>'返回void,返回关键字一定不能跟一个对象前pression
所以,我的问题是,是否有可能如果是运行在这个时尚同步的方法,并且,我究竟做错了什么?我的目标是,该方法可以被称为像这样:
VAR数据=等待LoadData(PARAMS);
当您添加服务引用有产生操作的异步版本的选项。
这是(旧的)APM模式(IAsyncResult的,BeginMethod,EndMethod)。你可以连接到这一点异步/等待与:
VAR任务= Task.Factory.FromAsync(BeginGetResultCount,EndGetResultCount,myParams);
当你有很多的电话,这是更好的,它不会浪费这么多线程等待I / O。
I have a WCF service, called by a service client. I'd like to use the async / await constructs to wrap the call to this; however, the service and service client are .NET3.5. My solution to this is as follows:
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);
}
});
}
I get the compile error:
Since 'System.Action<System.Threading.Tasks.Task>' returns void, a return keyword must not be followed by an object expression
So, my question is, is it possible to run a synchronous method in this fashion and, if so, what am I doing wrong? My objective is that the method can be called like so:
var data = await LoadData(params);
When you add the Service Reference there is an option to generate async versions of the operations.
This is the (older) APM pattern (IAsyncResult, BeginMethod, EndMethod). You can hook this into async/wait with FromAsync :
var task = Task.Factory.FromAsync(BeginGetResultCount, EndGetResultCount, myParams);
When you have many calls this is better, it doesn't waste so many threads to wait for I/O.
这篇关于使用异步任务调用同步WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!