使用异步任务调用同步WCF服务 [英] Using asynchronous tasks to call synchronous WCF service

查看:140
本文介绍了使用异步任务调用同步WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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)。你可以连接到这一点异步/等待与 FromAsync

  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服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆