C#, .NET and Windows Embedded Standard 7 recipes for developers

Main menu

Post navigation

Full Duplex WCF connection

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:

/// Alive round trip answer. This will be send for an each alive round trip request from the client side.

/// &lt;/summary&gt;

[OperationContract(IsOneWay=true)]

voidSetAliveRoundTrip();

/// &lt;summary&gt;

/// Update "hello world"

/// &lt;/summary&gt;

/// &lt;param name="helloWorld"&gt; Hello world content with server time &lt;/param&gt;

[OperationContract(IsOneWay=true)]

voidUpdateHelloWorld(stringhelloWorld);

#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…!