Implementing a Web Service with Windows Communication Foundation is really easy. You need only a few clicks for having fun and enjoying a cup of coffee after successfuel implementation.
But what is, when you have the requirement for implementing a stable and fully duplex communication including the server may send indepently messages to any specific connected client at any time? The client and server should also detect broken connections and reconnect automatically. Thats a little more complex and you can not implement by one click. Too bad for enjoying your coffee in a few minutes.
For illustration purposes I have created a sample project. The client may request at any time a “Hello World” from server. Every 30 seconds, the server sends a “Hello World” to every connected client. In this sample application you see the mechanism how you can implement a callback contract and a duplex communication.
The screenshot above shows the hosted server and three clients, which are connected to the server. They are hosted in a WPF-application and MVVM based on PRISM libraries:
The implementation has the following interfaces:
using System; using System.ServiceModel; namespace Comm.Wcf.Interfaces { /// <summary> /// WCF interface for Data /// </summary> [ServiceContract( CallbackContract = typeof (IDataCallback), SessionMode = SessionMode.Required)] public interface IData { #region Methods /// <summary> /// Request alive round trip /// </summary> /// <param name="id"> Unique identification number </param> [OperationContract(IsOneWay = true)] void RequestAliveRoundTrip(Guid id); /// <summary> /// Requests a "hello world" from server. /// </summary> /// <param name="id"> Unique identification number </param> [OperationContract(IsOneWay = true)] void RequestHelloWorld(Guid id); /// <summary> /// Subscribe client /// </summary> /// <param name="id"> Unique identification number </param> [OperationContract(IsOneWay = true)] void Subscribe(Guid id); /// <summary> /// Unsubscribe client /// </summary> /// <param name="id"> Unique identification number </param> [OperationContract(IsOneWay = true)] void Unsubscribe(Guid id); #endregion } }
using System.ServiceModel; namespace Comm.Wcf.Interfaces { /// <summary> /// WCF Interface for Data callback /// </summary> public interface IDataCallback { #region Methods /// <summary> /// Alive round trip answer. This will be send for an each alive round trip request from the client side. /// </summary> [OperationContract(IsOneWay = true)] void SetAliveRoundTrip(); /// <summary> /// Update "hello world" /// </summary> /// <param name="helloWorld"> Hello world content with server time </param> [OperationContract(IsOneWay = true)] void UpdateHelloWorld(string helloWorld); #endregion } }
You can see for a fully duplex and asynchronous communication you will need to mark all operations as “OneWay”. If you have big files I recommend to transmit a separate service by a stream contract, because a stream needs “TwoWay”. If the transmission is complete you may mark the transmission by a seperate call.
If you are a beginner in WCF you will see a lot of code lines which may a little bit confusing at first and are for a simple “Hello World” an overkill. But when you want to implement a stable application this project may be a good start for implementing your requirements.
So, I gave you a short overview. Now you should be ready to download the sample project. And enjoy your coffee…!