Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does the client perform WCF asynchronous calls

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how the client performs WCF asynchronous calls". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how the client performs WCF asynchronous calls".

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. Reasonable use of asynchronous invocation of services can effectively improve system performance and reasonably allocate the execution of tasks. Especially for UI applications, it can improve the response speed of UI and improve the user experience. In the applications I write, if the downloaded files are large, it is necessary to do so asynchronously. WCF is called asynchronously as follows:

BasicHttpBinding binding = new BasicHttpBinding (); binding.SendTimeout = TimeSpan.FromMinutes (10); binding.TransferMode = TransferMode.Streamed; binding.MaxReceivedMessageSize = 9223372036854775807; EndpointAddress address = new EndpointAddress ("http://localhost:8008/DocumentExplorerService"); ChannelFactory factory = new ChannelFactory (binding,address); m_service = factory.CreateChannel (); …" IAsyncResult result = m_service.BeginTransferDocument (doc,null,null); result.AsyncWaitHandle.WaitOne (); Stream stream = m_service.EndTransferDocument (result); that's all of the article "how the client performs WCF Asynchronous calls". 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report