我找到了解释如何实现异步 WCF 服务操作的文档:
https://msdn.microsoft.com/en-us/library/ms731177(v=vs.110).aspx
这让我想知道为什么(或什么时候)我想让我的服务操作异步,而客户端可以自己生成操作的异步版本。当客户端生成操作的异步版本时,是否就好像服务本身已经实现了异步操作一样?
此外,在文档的示例代码中,结束方法没有 OperationContractAttribute。为什么?
请您参考如下方法:
我认为您的问题的答案就在您提供的链接之后。 在这里 ( https://msdn.microsoft.com/en-us/library/ms734701%28v=vs.110%29.aspx ) 你可以看到:
Use an asynchronous approach in a service operation implementation if the operation service implementation makes a blocking call, such as doing I/O work. When you are in an asynchronous operation implementation, try to call asynchronous operations and methods to extend the asynchronous call path as far as possible. For example, call a BeginOperationTwo() from within BeginOperationOne().
因此,当您的服务使用其他服务(例如 Redis)或下载任何内容时,以异步方式实现这些操作绝对是非常有意义的,并且不会阻塞服务器的工作线程。工作线程的数量是有限的,有时你的服务器会卡住,因为所有线程都在服务调用。在异步实现的情况下,您可以利用 usin I/O 完成端口和来自单独线程池的适当 I/O 线程。 I/O completion ports是 Windows 中非常有效的机制,有助于避免资源浪费。
