Create and use a WCF Service (duplex mode)
Yesterday, we released the create and use of the WCF Service (request response mode). Today, we continue to learn how to create and use the WCF Service in duplex mode. The steps and Code are as follows.
The first step is to define the Service Contract. Note that the callback Contract must be specified in the ServiceContract feature.
// Service contract using System. serviceModel; namespace WcfServiceLibrary1 {[ServiceContract (Namespace = "http://www.zuowenjun.cn", CallbackContract = typeof (ICallback)] public interface IHello {[OperationContract (IsOneWay = true)] void SetName (string name) ;}// callback contract (because the callback contract is essentially a service contract, the definition method is basically the same as that in the general sense. // The difference is that when defining a service contract, [ServiceContract (CallbackContract = typeof (ICallback)] indicates that ICallback is a service contract, therefore, the ServiceContractAttribute feature is no longer required for ICallback.) using System. serviceModel; namespace WcfServiceLibrary1 {public interface ICallback {[OperationContract (IsOneWay = true)] void ShowHello (string name );}}
Step 2: implement the service contract. Here, the callback contract is obtained through OperationContext. Current. GetCallbackChannel ().
using System.ServiceModel;namespace WcfServiceLibrary1{ [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] public class HelloService:IHello { public void SetName(string name) { ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>(); callback.ShowHello(name); } }}
Note: ConcurrencyMode = ConcurrencyMode. Reentrant sets the concurrency mode. Both Reentrant and Multiple can be used for Multiple concurrency. The default value is Single.
Step 3: There are many methods to create a service host Program. Here I use the common configuration method.
1. CONFIG file Configuration:
(Note: 1. in the predefined WCF binding type, both WSDualHttpBinding and NetTcpBinding provide support for duplex communication, but the two have essential differences in the implementation mechanism of duplex communication. WSDualHttpBinding is based on the HTTP transmission protocol, while the HTTP protocol itself is based on the request-reply transmission protocol, and the HTTP-based channel is essentially one-way. WSDualHttpBinding actually creates two channels, one for communications between the client and the server, and the other for communications between the server and the client, indirectly providing duplex communication. NetTcpBinding is fully based on TCP protocol that supports duplex communication.
2. If the binding type uses NetTcpBinding, the address strain will be: net. tcp: // 127.0.0.1: 10800/HelloService, and behaviorConfiguration cannot be configured)
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> </service> <service name="WcfServiceLibrary1.HelloService"> <endpoint address="http://127.0.0.1:10800/HelloService" binding="wsDualHttpBinding" contract="WcfServiceLibrary1.IHello"></endpoint> </service> </services> </system.serviceModel></configuration>
2. Code:
Using System; using WcfServiceLibrary1; using System. serviceModel; using System. serviceModel. description; namespace ConsoleApplicationHost {class Program {static void Main (string [] args) {BuildHelloServiceHostByConfig ();} static void BuildHelloServiceHostByConfig () {using (ServiceHost host = new ServiceHost (typeof (HelloService) {host. opened + = (s, e) => {Console. writeLine ("HelloService has been started, press ENTER Key to terminate the service! ") ;}; Host. Open (); Console. ReadLine ();}}}}
Step 4: The client calls the WCF Service
Note: In the client program, you must first create or reference the WCF Service class library, and then create a class that implements the callback contract.
Implement callback contract:
Using System; using WcfServiceLibrary1; namespace ConsoleApplicationClient {public class HelloCallBack: ICallback {public void ShowHello (string name) {Console. WriteLine ("Hello! {0}. Welcome to IT Wenjun Community network, address: www.zuowenjun.cn ", name );}}}
1. CONFIG file Configuration:
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <bindings> <wsDualHttpBinding> <binding name="HelloServiceBinding" clientBaseAddress="http://127.0.0.1:10801/HelloServiceCallback"></binding> </wsDualHttpBinding> </bindings> <client> <endpoint name="HelloService" address="http://127.0.0.1:10800/HelloService" binding="wsDualHttpBinding" contract="WcfServiceLibrary1.IHello"> </endpoint> </client> </system.serviceModel></configuration>
2. Code:
Using System; using System. ServiceModel; using WcfServiceLibrary1; namespace ConsoleApplicationClient {class Program {static void Main (string [] args) {CallHelloService (); Console. WriteLine ("ends by any key. "); Console. read ();} static void CallHelloService () {Console. write ("Enter your network name:"); string input = Console. readLine (); InstanceContext instanceContext = new InstanceContext (new HelloCallBack (); using (DuplexChannelFactory <IHello> channel = new DuplexChannelFactory <IHello> (instanceContext, "HelloService ")) {IHello proxy = channel. createChannel (); using (proxy as IDisposable) {proxy. setName (input ); Console. ReadLine (); // note that this statement must be retained to prevent calling the proxy's Dispose method, because this method will attempt to close the underlying TCP connection. Because the callback operation on the server will also use this TCP connection, if you attempt to close the network connection before the callback operation is completed, the callback will fail to be executed }}}}}
We recommend that you add the IsOneWay = True feature for the method if no return value is returned when defining the service method, so that the client does not have to wait for a response request, although ConcurrencyMode = ConcurrencyMode is set. reentrant, but if you use the WINFORM client, a TIMEOUT error will occur. For details, see: My WCF journey (6 ): reasons and solutions for TimeoutException when Duplex Service is called in Winform Application
Note: This article is based on the following author's article:
My WCF journey (3): Implement duplex communication in WCF
Article published on my personal website: http://www.zuowenjun.cn/post/2015/03/25/133.html