Async CTP - 如何使用 async/await 调用 wcf 服务? [英] Async CTP - How can I use async/await to call a wcf service?
问题描述
如果我调用 WCF 服务方法,我会这样做:
If I call a WCF service method I would do something like this:
proxy.DoSomethingAsync();
proxy.DoSomethingAsyncCompleted += OnDoSomethingAsyncCompleted;
如何使用新的 async
ctp 做同样的事情?我想我需要像 proxy.DoSomethingTaskAsync
或 proxy.DoSomethingAsync().ToTask()
之类的东西?Web 服务调用需要返回一个 Task
才能使用 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 函数(开始/结束)转换为与新的 async 关键字兼容的函数,例如实例:
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: est.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()
请参阅此主题CTP讨论组以获取更多信息
See this thread on the CTP discussion group for more info
这篇关于Async CTP - 如何使用 async/await 调用 wcf 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!