问题描述
如果我调用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()
请参阅在CTP讨论组获取更多信息。
See this thread on the CTP discussion group for more info
这篇关于异步CTP - 我如何使用异步/等待调用WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!