I always thought that I didn't understand duplex, but I really felt very disappointed. I learned some features of duplex on the Internet and directly went to the Code for future projects:
Service Layer:
Define an IDuplexHello Service Interface
[ServiceContract (Name = "DuplexHello", Namespace = "http://microsoft.wcf.documentation", CallbackContract = typeof (IHelloCallbackContract), // set the callback service type SessionMode = SessionMode. required)] public interface IDuplexHello {[OperationContract] void Hello (string greeting );}
Implement DuplexHello. cspublic class DuplexHello: IDuplexHello {public void Hello (string greeting) {Console. writeLine ("Caller sent:" + greeting); Console. writeLine ("Session ID:" + OperationContext. current. sessionId); Console. writeLine ("Waiting two seconds before returning call. "); Thread. sleep (2000); var callerProxy = OperationContext. current. getCallbackChannel <IHelloCallbackContract> (); var response = "Service object" + this. getHashCode (). toString () + "received:" + greeting; Console. writeLine ("Sending back:" + response); callerProxy. reply (response );}}
Define the callback interface (there is no need to add servicecontract to the callback service function, because it is included in the IDuplexHello service type)
Public interface IHelloCallbackContract {[OperationContract (IsOneWay = true)] // remember to add operationcontract void Reply (string responseToGreeting );}
The callback interface implementation class (HelloCallbackContract. cs) I implemented it directly at the service layer. Of course, in most cases, the project will be implemented at the Customer service end. I am here to facilitate public class HelloCallbackContract: IHelloCallbackContract {public void Reply (string responseToGreeting) {Console. writeLine (responseToGreeting );}}
Host the service to the console application (using code implementation ):
Private static void Main (string [] args) {using (var host = new ServiceHost (typeof (DuplexHello), new Uri (" http://localhost:991/DuplexHello ") // Add the base address, which is the address in the client. In addition, the port number cannot be the same as the port number {host. addServiceEndpoint (typeof (IDuplexHello), new NetTcpBinding (), "net. tcp: // localhost: 999/DuplexHello "); var metadatbehavior = host. description. behaviors. find <ServiceMetadataBehavior> (); if (metadatbehavior = null) {metadatbehavior = new ServiceMetadataBehavior () {HttpGetEnabled = true}; host. description. behaviors. add (metadatbehavior);} host. opened + = delegate {Console. writeLine ("service started") ;}; host. open (); Console. read ();}
The client layer also uses a console application:
First, run the host (find the host-layer resource folder bin-> debug: host.exe)to run host.exe when updating the wcfservice; otherwise, the server cannot be connected,
Then, enter http: // localhost: 991/DuplexHello Application Service in the address bar of the service reference.
After the application is completed, the client app. config automatically generates the following code:
<configuration> <system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_DuplexHello" /> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://localhost:999/DuplexHello" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_DuplexHello" contract="ServiceReference1.DuplexHello" name="NetTcpBinding_DuplexHello"> <identity> <userPrincipalName value="objectboy-PC\objectboy" /> </identity> </endpoint> </client> </system.serviceModel></configuration>
Code in the client console application:
Private static void Main (string [] args) {var hellocallbackcontract = new HelloCallbackContract (); var instanceContext = new InstanceContext (hellocallbackcontract ); // instantiate the client service context var duplexChannelFactory = new DuplexChannelFactory <IDuplexHello> (instanceContext, new NetTcpBinding (); // instantiate the duplex channel and bind it to tcp communication, note that ChannelFactory cannot be used, because this is a duplex var endpointaddress = new EndpointAddress ("net. tcp: // localhost: 999/DuplexHello "); var proxy = duplexChannelFactory. createChannel (endpointaddress); // create and send the message to the specified message channel using (proxy as IDisposable) {proxy. hello ("cccccccccccccccc");} Console. read ();}
Customer service output:
Server output: