WCF preliminary research-13: the WCF client creates a callback object for the duplex service, wcf-13
Preface:
InWCFPreliminary Study-5: WCFDuplex Communication in message exchange mode)In the blog, I explained An Application Scenario of duplex communication service, namely the subscription and publishing modes, I will explain how the WCF client creates a callback object for the duplex service through an example of message sending.
A duplex Service specifies a callback protocol, which must be implemented by the client application to provide a callback object that the service can call according to the Protocol requirements. Although the callback object is not a complete service (for example, you cannot use the callback object to start a channel), these callback objects can be considered a service for implementation and configuration.
The client of the duplex service must:
- Implements a callback protocol class.
- Create an instance of the callback protocol implementation class and use this instance to create the System. ServiceModel. InstanceContext object passed to the WCF client constructor.
- Call the operation and process the operation callback.
In addition to publicly supporting the functions required for callback (including callback service configuration), duplex WCF client objects have the same functions as their non-duplex counterparts.
Example:
- The Service contract defines a sending method, which is IsOneWay = true for the client to call and Send messages to the server. The Service also provides the callback interface IMessageExchangeCallback for duplex communication. This interface defines the method Receive for sending messages to the client after the server receives the messages. After the client sends the messages to the server, the server calls the callback method to send messages to the client.
- The Client needs to implement the CallBackHandler class of the duplex protocol callback interface and implement the Receive method.
- The WCF client generated for the duplex protocol must provide an InstanceContext class during the construction. This InstanceContext class is used as the location of the object that implements the callback interface and processes the message sent back from the service. The InstanceContext class is constructed using an instance of the CallbackHandler class. This object processes messages sent from the service to the client through the callback interface.
WCFThe client creates a callback object for the duplex service.Example:
- The solution is as follows:
- Engineering Structure Description:
The IMessageExchange. cs code is as follows:
using System.ServiceModel;using System.Collections.Generic;using System.Runtime.Serialization;namespace Service{ [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMessageExchangeCallback))] public interface IMessageExchange { [OperationContract(IsOneWay=true)] void Send(string message); } public interface IMessageExchangeCallback { [OperationContract(IsOneWay = true)] void Receive(string message); }}
The MessageExchange. cs code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. serviceModel; namespace Service {[ServiceBehavior (InstanceContextMode = InstanceContextMode. perSession)] public class MessageExchange: IMessageExchange {public void Send (string message) {Console. writeLine ("the message sent by the server listening client:" + message); Callback. receive (message);} IMessageExchangeCallback Callback {get {return OperationContext. current. getCallbackChannel <IMessageExchangeCallback> ();}}}}
Note: The callback contract interface IMessageExchangeCallbackInReceiveThe method is implemented on the client, so if you need to call the callback method on the server, you must obtain it through the instance context of the current operation, that is,CallbackObject.
2. Host: console application. Provides a service host Program to add references to the Srevice assembly. After the configuration file and code are complete, the service can be hosted.
The Code of Program. cs is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using Service; using System. serviceModel; namespace Host {class Program {static void Main (string [] args) {using (ServiceHost host = new ServiceHost (typeof (MessageExchange) {host. opened + = delegate {Console. writeLine ("the service has been started. Press any key to terminate! ") ;}; Host. Open (); Console. Read ();}}}}View Code
The code for App. config is as follows:
<? Xml version = "1.0"?> <Configuration> <system. serviceModel> <services> <service name = "Service. messageExchange "behaviorConfiguration =" mexBehavior "> 3. Client: console application. Client Program, start the Host of the Service bearer program, add a reference to the service address http: // localhost: 1234/MessageExchange/, and change the namespace
MessageExchangeServiceRef, and then implement the IMessageExchangeCallback interface and call the service method in Program. cs. The Code of Program. cs is as follows:
Using System; using Client. messageExchangeServiceRef; using System. serviceModel; namespace Client1 {public class CallBackHandler: IMessageExchangeCallback {public void Receive (string message) {Console. writeLine ("message received by the client listening server:" + message) ;}} class Program {static void Main (string [] args) {InstanceContext instanceContext = new InstanceContext (new CallBackHandler (); MessageExchangeClient proxy = new MessageExchangeClient (instanceContext); proxy. send ("Wcf Duplex"); Console. read ();}}}
The running result is as follows:
Summary:
- This article simulates a client to send a message to the server. After the server receives the message, it displays the received message on the client. I hope to use this example to get a step-by-step understanding of duplex communication. I will parse the instance and session in the following blog post.