In WCF, HTTP and Tcp Protocols are often used for duplex communication. The corresponding protocols are WsDualHttpBinding and NetTcpBinding. HTTP is based on the application layer protocol, while Tcp is based on the transport layer protocol. Tcp uses three handshakes to establish a reliable connection between the client and the server. Both the client initiating the request and the server of the callback client can use one connection. When HTTP is used, the connection is closed after a request is sent from the client to the server and the server returns a reply to the client. Due to the non-connectivity of HTTP, the duplex Based on WsDualHttpBinding may be slightly different from NetTcpBinding when the server calls back the client. 1. NetTcpBinding duplex 1.1 contract part: 1.1.1 contract Interface Definition: [ServiceContract (CallbackContract = typeof (ICallback)]
Public interface ICalculator
{
[OperationContract]
Void Add (double x, double y );
} 1.1.2 callback interface Definition: public interface ICallback
{
[OperationContract]
Void Display (double x, double y, double result );
} 1.2 service implementation: public void Add (double x, double y)
{
Console. WriteLine ("START Add Calculation ");
ICallback callback = OperationContext. Current. GetCallbackChannel <ICallback> ();
Callback. Display (x, y, x + y );
Console. WriteLine (string. Format ("{0}, {1}", x, y ));
}
The Service obtains the callback proxy class for the client through the OperationContext. Current. GetCallbackChannel <T> generic method, and calls back the public T GetCallbackChannel for the client <T> ()
{
If (this. channel! = Null )&&! This. IsUserContext)
{
Return (T) this. channel. Proxy;
}
Return default (T );
}
1.3 Server Configuration:
<System. serviceModel>
<Services>
<Service name = "Services. CalculatorServices">
<Endpoint address = "Calaulator" binding = "netTcpBinding" contract = "Contract. ICalculator"> </endpoint>
<Host>
<BaseAddresses>
<Add baseAddress = "net. tcp: // 127.0.0.1: 6688"/>
</BaseAddresses>
</Host>
</Service>
</Services>
</System. serviceModel>
1.4 client part: Implementation of the callback interface: public void Display (double x, double y, double result)
{
Console. WriteLine (string. Format ("{0} + {1} = {2}", x, y, result ));
}
Client configuration: <system. serviceModel>
<Client>
<Endpoint address = "net. tcp: // 127.0.0.1: 6688/Calaulator" binding = "netTcpBinding" contract = "Contract. ICalculator" name = "calculatorTcp"> </endpoint>
</Client>
</System. serviceModel>
Interface call: InstanceContext context = new InstanceContext (new CallBack ());
Using (var channelFactory = new DuplexChannelFactory <ICalculator> (context, "calculatorTcp "))
{
ICalculator proxy = channelFactory. CreateChannel ();
Proxy. Add (1.0, 2.0 );
Console. ReadLine ();
} The result is as follows:
Client:
2. WsDualHttpBinding implements duplex.
Operating System: XP SP3. IIS Version: Take the same contract as above 5.1 as an example. In the packet or request-response mode, if you change the protocol, you only need to change the binding used. We changed the client configuration to the following: <system. serviceModel>
<Client>
<Endpoint address = "http: // 127.0.0.1: 8866/Calaulator/ws" binding = "wsDualHttpBinding" contract = "Contract. ICalculator" name = "calculatorWsDual"> </endpoint>
</Client>
</System. serviceModel>
Then use calculatorWsDual to regenerate ChannelFactory:
Using (var channelFactory = new DuplexChannelFactory <ICalculator> (context, "calculatorWsDual") Run debugging: The following exception occurs: the solution is to configure binding behavior for the Endpoint, as follows:
<System. serviceModel>
<Bindings>
<WsDualHttpBinding>
<Binding name = "myWsDualBinding" clientBaseAddress = "http: // 127.0.0.1: 3636/Service/CallbackService"> </binding>
</WsDualHttpBinding>
</Bindings>
<Client>
<Endpoint address = "http: // 127.0.0.1: 8888/Service/Calculator" binding = "wsDualHttpBinding" contract = "Contracts. ICalculator "name =" calculatorService "bindingConfiguration =" myWsDualBinding "/>
</Client>
</System. serviceModel>
In this way, the problem can be solved.