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 to perform service operations on WCF asynchronous invocations

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

Share

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

This article focuses on "how to operate a service on WCF asynchronous invocation". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to perform service operations on WCF asynchronous invocations.

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 use WCF asynchronously to invoke the service operation designed by me, although the service contract interface of the client needs to be modified, it will 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.

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); now that you have a better understanding of "how to operate a service on an WCF asynchronous call", you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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