使用 C# 异步调用 WCF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/781932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Async call to WCF with C#?
提问by Phillip
In C#, how do I make an async call to a WCF Web Service? I have a Service Reference set up to generate async. I also modified my calls to use { WebServiceObject.Begin* () } but there are two parameters at the end, 'AsyncCallback' and 'object asyncState'. What are these and how do I use them?
在 C# 中,如何对 WCF Web 服务进行异步调用?我有一个服务参考设置来生成异步。我还修改了我的调用以使用 { WebServiceObject.Begin* () } 但最后有两个参数,“AsyncCallback”和“object asyncState”。这些是什么以及如何使用它们?
Thanks,
谢谢,
采纳答案by thijs
See MSDN here; http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx
在此处查看 MSDN;http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx
here; http://msdn.microsoft.com/en-us/library/ms228969.aspx
这里; http://msdn.microsoft.com/en-us/library/ms228969.aspx
here; http://msdn.microsoft.com/en-us/library/ms228975.aspx
这里; http://msdn.microsoft.com/en-us/library/ms228975.aspx
and here; http://msdn.microsoft.com/en-us/library/86wf6409.aspx
和这里; http://msdn.microsoft.com/en-us/library/86wf6409.aspx
Basicly in Begin* you set an callback, that callback is called when the operation has completed. There you call End* to retrieve the appropriate data.
基本上在 Begin* 中您设置了一个回调,该回调在操作完成时被调用。在那里你调用 End* 来检索适当的数据。
回答by Ihar Voitka
Callback is called when operation is completed, so you can call End* and grab return value or exception if any. asyncState is just a value for matching in callback if you use same callback method in several places. Here is a description of Async design pattern - http://msdn.microsoft.com/en-us/library/aa719595(VS.71).aspx
操作完成时调用回调,因此您可以调用 End* 并获取返回值或异常(如果有)。如果您在多个地方使用相同的回调方法,则 asyncState 只是用于在回调中匹配的值。这是异步设计模式的描述 - http://msdn.microsoft.com/en-us/library/aa719595(VS.71).aspx
If you are creating GUI application, consider using another version - *Async method which is generated for each operation as well. It provides thread synchronization.
如果您正在创建 GUI 应用程序,请考虑使用另一个版本 - *Async 方法,它也为每个操作生成。它提供线程同步。
回答by Marc Gravell
You might also want to look at Async without the Pain
你可能还想看看Async without the Pain