In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to achieve asynchronous calls in WCF". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to achieve asynchronous calls in WCF".
Asynchronous calls are indispensable in programming. Gain a little bit of experience in your work. Now let's take a look at WCF asynchronous calls. The difference between WCF and Web Service is that when we define the operation of a service contract, whether we create a service proxy object through ChannelFactory or generate a service proxy object by default in SvcUtil, the client cannot directly implement asynchronous invocation when invoking these proxy objects. For example, for the following service operation definition:
[OperationContract] Stream TransferDocument (Document document)
When we call the methods of the proxy object, we cannot find the BeginTransferDocument () and EndTransferDocument () asynchronous methods corresponding to the TransferDocument () operation.
This design makes it impossible for us to invoke the operation of the service programmatically asynchronously unless we directly add the asynchronous method of the relevant operation when defining the service interface. However, this directly leads to the coupling between the design of the service and the way the method is invoked. A good framework design element is that the design and implementation of the service should be consistent regardless of the way the client is invoked (synchronous or asynchronous). For the designer of the service, the way the caller of the service invokes should not be considered at the beginning of the design. In other words, whether the service operation is asynchronous or not should be decided by the caller of the client. Therefore, everything related to WCF asynchronous calls should be relevant only to the client. WCF follows this rule.
In the application I write, a service operation that transfers document files is exposed. I don't know or care whether the client that invokes the operation is asynchronous. Therefore, the service operation definition described above is completely correct.
So how on earth should the client perform WCF asynchronous calls? This problem becomes worse if you get the service proxy object programmatically. Because I formed the definition of the service contract into a separate assembly and referenced it directly on the client. However, there is no definition of asynchronous methods in such service contract assemblies. Therefore, I need to modify the service definition on the client side to increase the asynchronous method of the operation. This undoubtedly poses an obstacle to the reuse of service contracts. At the very least, we need to maintain a service contract with an asynchronous method on the client side.
Fortunately, when the client decides to invoke the service operation designed by me asynchronously, the service contract interface of the client needs to be modified, but it does not affect the contract definition of the server. Therefore, the contract definition on the server side can remain unchanged, while on the client side, the interface definition can be modified as follows:
[ServiceContract] public interface IDocumentsExplorerService {[OperationContract] Stream TransferDocument (Document document); [OperationContract (AsyncPattern = true)] IAsyncResult BeginTransferDocument (Document document, AsyncCallback callback, object asyncState); Stream EndTransferDocument (IAsyncResult result);}
Note that on the BeginTransferDocument () method, the value of the AsyncPattern attribute must be set to true in OperationContractAttribute because its default value is false.
The above is all the content of the article "how to implement asynchronous invocation of WCF". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.