异步CTP - 我如何使用异步/等待调用WCF服务? [英] Async CTP - How can I use async/await to call a wcf service?

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

问题描述

如果我调用WCF服务方法,我会做这样的事情:

If I call a WCF service method I would do something like this:

proxy.DoSomethingAsync();
proxy.DoSomethingAsyncCompleted += OnDoSomethingAsyncCompleted;

我怎么能做到用新的异步 CTP一样吗?
我想我会需要像 proxy.DoSomethingTaskAsync proxy.DoSomethingAsync()。ToTask()? Web服务调用需要返回一个任务< T> ,以便能够使用的await 关键词,但如何? ?

How could I do the same using the new async ctp? I guess I would need something like proxy.DoSomethingTaskAsync or proxy.DoSomethingAsync().ToTask()? The web service call needs to return a Task<T> to be able to use the await keyword, but how??

推荐答案

在CTP有工厂方法做转向常规APM功能(开始/结束)到那些与新的异步关键字兼容的工作,为例如:

In the CTP there are factory methods that do the work of turning regular APM functions (Begin/End) into ones that are compatible with the new async keyword, for instance:

Stream s = new FileStream("C:\test.txt", FileMode.CreateNew);
byte []buffer = new byte[100];
int numBytesRead = await Task<int>.Factory.FromAsync(s.BeginRead, s.EndRead, buffer, 0, buffer.Length, null);

所以你的情况,你可以做等价,然后你再调用它像这样:

So in your case you can do the equivalent and then you'd then call it like so:

async proxy.DoSomethingTaskAsync()

请参阅this螺纹在CTP讨论组获取更多信息。

See this thread on the CTP discussion group for more info

这篇关于异步CTP - 我如何使用异步/等待调用WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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