Duplex mode
Description: duplex mode is built on the basis of the reply mode and one-way mode, which realizes the client-server mutual invocation.
Call each other: In the past we just call the server on the client side, and then the service side has a return value to return the client, and the mutual call is not only the client calls the server, but also the server can invoke the client's method.
1. Add the WCF service Service2.svc, and define the interface for the callback, server-side interface IService2.cs:
Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using System.servicemodel;using system.text;namespace wcfservice1{ //callbackcontract = typeof (IService2CallBack) The interface type that defines the callback [ServiceContract (callbackcontract = typeof (Iservice2callback))] public interface IService2 { [OperationContract] string showname (string name); Callback interface in which the method on the client implements public interface Iservice2callback { //isoneway = True to start one-way mode, the mode method cannot have a return value [OperationContract (IsOneWay = true)] void printsomething (String str);} }
2. Service-Side Implementation SERVICE2.SVC
Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using system.servicemodel;using system.text;namespace wcfservice1{Public class Service2:iservice2 { Iservice2callback callback = null;//Callback interface type public Service2 () { //Gets the client instance that invokes the current operation callback = Operationcontext.current.getcallbackchannel<iservice2callback> (); } <summary>// services called by the client///</summary>// <param name= "name" ></param> //<returns></returns> public string ShowName (string name) { callback. Printsomething (name); Return "Server call client, WCF service, display name: Xsj ...";}}
3. Server-side configuration, Web. config adds configuration in System.ServiceModel,
There are 4 types of bindings that support callbacks: Wsdualhttpbinding, NetTcpBinding, NetNamedPipeBinding, netpeertcpbinding.
Here we use wsdualhttpbinding as an example:
<endpoint address= "" binding= "wsdualhttpbinding" contract= "Wcfservice1.iservice2" ></endpoint>
<system.serviceModel> <services> <service name= "Wcfservice1.service2" > < Endpoint address= "" binding= "wsdualhttpbinding" contract= "Wcfservice1.iservice2" ></endpoint> </service> </services></system.serviceModel>
WCF Duplex mode