In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to achieve two-way communication in WCF". In daily operation, I believe many people have doubts about how to achieve two-way communication in WCF. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve two-way communication in WCF". Next, please follow the editor to study!
Callback during request
This is a typical form of duplex message exchange pattern. When the client invokes the service, a callback object is attached to the client. During the processing of the service, the client's operation is called back through the callback object attached by the client (which is actually the proxy object that invokes the callback service). The whole process of message exchange is actually composed of two basic message exchanges, one is the normal service request of the client, and the other is the callback of the server to the client. The two can use request-reply mode, or one-way (One-way) MEP for message exchange. Figure 1 depicts the process in which both service invocation and callback adopt the request-reply MEP.
Figure 1
We follow the example of computing services. Before that, we used to call CalculuateService to get the calculation results directly, and output the calculation results through the console. In this example, we will invoke the service in a completely different way and output the result: we call CalculuateService through an one-way (One-way) mode (that is, it is impossible for the client to get the calculation result by replying to the message), and the server prints the calculation result on the client side through Callback after completing the calculation result.
One: define service contract and callback contract
First of all, we define the service contract. As usual, we define the service contract through the interface (ICalculator), which acts on the Add operation of the specified addition operation. We define the operation as an one-way operation through the IsOneway attribute of the OperationContractAttribute feature, which means that the client only sends a request for an operation to the server and will not get any operation result by replying to the message.
[ServiceContract (Namespace = "http://www.artech.com/", CallbackContract = typeof (ICallback))] / / the type of callback contract is specified through the CallbackContract attribute of the ServiceContractAttribute attribute. Public interface ICalculator {[OperationContract (IsOneWay = true)] void Add (double x, double y);} / callback contract public interface ICallback / / since the definition of ICalculator has already indicated that ICallback is a service contract through [ServiceContract (CallbackContract=typeof (ICallback))], ICallback no longer needs to add ServiceContractAttribute features. {[OperationContract (IsOneWay = true)] / / the server does not need the return value of the callback, so the callback operation is also set to an one-way method. Void DisplayResult (double x, double y, double result);} II: implement the service
In the service CalculatorService that implements the service contract ICalculator defined above, the Add operation is implemented, and the operation and result display are completed. The result shows that it is achieved through a callback, so you need to rely on the callback object provided by the client (which is specified when the client invokes CalculatorService and will be discussed when you introduce the implementation of the client code). In WCF, the callback object is obtained through the GetCallback method of the current OperationContext (T represents the type of callback contract).
Public class CalculatorService: ICalculator {public void Add (double x, double y) {double result = x + y; ICallback callback = OperationContext.Current.GetCallbackChannel (); callback.DisplayResult (x, y, result);}}
Note: OperationContext is a very important and useful object in WCF, which represents the context in which service operations are executed. We can get the current OperationContext through the static attribute Current (OperationContext.Current). With OperationContext, we can get or set some contexts on the server or client, such as adding SOAP headers for outgoing message messages and HTTP headers (such as Cookie) on the client side. On the server side, the SOAP header and HTTP header set on the client side can be obtained through OperationContex. For more information about OperationContext, refer to the MSDN online documentation.
Three: service boarding
We host CalculatorService through a console application and define all the service hosting parameters in the configuration file. Because duplex communication depends on a duplex channel stack, that is, on a binding that can support duplex communication, we choose NetTcpBinding here.
Note: in the predefined binding types of WCF, both WSDualHttpBinding and NetTcpBinding provide support for duplex communication, but there are essential differences in the implementation mechanism of duplex communication. WSDualHttpBinding is based on HTTP transport protocol, while HTTP protocol itself is based on request-reply transport protocol, and HTTP-based channels are essentially one-way. WSDualHttpBinding actually creates two channels, one for client-to-server communication and the other for server-to-client communication, thus indirectly providing the implementation of duplex communication. NetTcpBinding is based entirely on the TCP protocol that supports duplex communication.
Using (ServiceHost host = new ServiceHost (typeof (CalculatorService) {host.Open (); Console.Read ();} IV: implement callback contract and service invocation
The client program provides the implementation for the callback contract. In the following code, CalculateCallback implements the callback contract ICallback and outputs the operation result in the DisplayResult method.
Class CalculateCallback: ICallback {public void DisplayResult (double x, double y, double result) {Console.WriteLine ("x + y = {2} when x = {0} and y = {1}", x, y, result);}}
The next step is to implement the call to the duplex service, and here are the relevant configuration and managed programs. In service invokers, service proxy objects are created through DuplexChannelFactory, and the functions of DuplexChannelFactory and ChannelFactory are both a factory for creating service proxy objects, but DuplexChannelFactory is used specifically for the creation of service proxies based on duplex communication. Before creating a DuplexChannelFactory, create a callback object and wrap it through InstanceContext.
InstanceContext instanceContext = new InstanceContext (new CalculateCallback ()); using (DuplexChannelFactory channelFactory = new DuplexChannelFactory (instanceContext, "CalculatorService")) {ICalculator proxy = channelFactory.CreateChannel (); using (proxy as IDisposable) {proxy.Add (1,2); Console.Read ();}}
Note: if WsDualHttpBinding is used, port 80 is the exclusive listening port for IIS in IIS 5.x and previous versions, because the default port for the service listening address for callback is 80. WsDualHttpBinding defines a ClientBaseAddress that allows you to easily change the base address of the callback service. For the case we gave, we simply set clientBaseAddress to an available address (http://127.0.0.1:8888/ CalculatorService) with the following configuration
< bindings > < wsDualHttpBinding > < binding name = "MyBinding" clientBaseAddress = "http://127.0.0.1:8888/calculatecallback" />At this point, the study on "how to achieve two-way communication in WCF" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.