与WCF EAP使用自来​​水生成的代理 [英] Use TAP with wcf EAP generated proxies

查看:148
本文介绍了与WCF EAP使用自来​​水生成的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight 5应用程序,这使得使用WCF服务。已生成的代理客户端具有唯一的异步方法(默认,从对SL客户端产生时)。

I have a Silverlight 5 app, that makes use of a WCF service. The proxy client that has been generated has only asychronous methods (by default, when generating from the SL client).

我想利用基于任务的异步模式(TAP),现在在VS2012RC的。

I want to make use of the Task-based Asynchronous Pattern (TAP), now within VS2012RC.

什么是消耗从生成的客户端代理的异步方法,最好的方法?

What is the best approach to consume the async methods from the generated client proxy ?

(的问题是,该WCF代理生成器创建一个基于基于事件的异步模式(EAP),而不是TAP .... code)

(the issue is, that the WCF proxy generator creates code that is based on the Event-based Asynchronous Pattern (EAP) and not TAP....)

推荐答案

根据这份文件:
http://www.microsoft.com/en-us/download /details.aspx?id=19957

我已经找到了一个解决方案。

I have found a solution for this.

见下文code:

public class MyDataListProvider : IMyDataListProvider
{
    private <ObservableCollection<IMyData>> myDataList;

    public Task<ObservableCollection<IMyData>> GetMyData()
            {
                TaskCompletionSource<ObservableCollection<IMyData>> taskCompletionSource = new TaskCompletionSource<ObservableCollection<IMyData>>();

                MyWCFClientProxy client = new MyWCFClientProxy();

                this.myDataList.Clear();

                client.GetMyDataCompleted += (o, e) =>
                {
                    if (e.Error != null)
                    {
                        taskCompletionSource.TrySetException(e.Error);
                    }
                    else
                    {
                        if (e.Cancelled)
                        {
                            taskCompletionSource.TrySetCanceled();
                        }
                        else
                        {
                            foreach (var s in e.Result)
                            {
                                var item = new MyData();
                                item.Name = s.Name;
                                item.Fullname = s.Fullname;

                                this.myDataList.Add(item);
                            }

                            taskCompletionSource.TrySetResult(this.myDataList);
                        }
                    }
                };

                client.GetMyDataAsync();

                return taskCompletionSource.Task;
            }
}

客户端SL code:

Client SL code:

private async void SetMyDataList()
        {
            this.MyDataList = await this.myDataListProvider.GetMyData();
}

这篇关于与WCF EAP使用自来​​水生成的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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