WCF Chat Application

Introduction

As you all know, Microsoft has launched .NET 3.0 with four very powerful foundations.

WCF (Windows Communication Foundation)

WPF (Windows Presentation Foundation)

WF (Windows Workflow Foundation)

Windows Cardspace

There is already a lot of material available on what WCF, WPF, WF and cardspace are. I am not going in details of their technologies. While exploring WCF, I came across many interesting things that are newly introduced. In my previous chat application using remoting http://www.codeguru.com/Csharp/.NET/net_general/eventsanddelegates/article.php/c10215/, I used an interface that is distributed across the client and server. There was an abstract class that was implemented at the client side. When the server wants to send a message to the client, the server should have the list of clients and something that is common in all clients. An interface implementation was the best way to achieve this. There were serialization, channels, protocols and much more in .NET remoting.

In WCF things, are made pretty simple. The first and the most important thing is that you can have as many endpoints as you want, depending upon the requirements of your application. If you want your application to be used by a .NET client or a Java client, you can go for TCP binding and basic HTTP binding. What you need to do is add those many end points in the configuration file of the server and start the server. It is responsibility of WCF to give you performance benefits of communication with different clients. The basic communication protocol for WCF is SOAP. But, when you establish communication between the WCF service and the WCF client, the foundation uses binary over SOAP to give you optimum performance. I will say that you can have a WCF service with everything under one roof. Currently, I am also in the process of exploring more about WCF and specifically the bindings. There are some features that are required but I do not have any idea about how to get them from netPeerTcpBinding and PeerResolvers.

About the Application

The simple WCF chat application I have written uses netTcpPeerBinding. This binding makes it very easy to create a simple intranet chat application that can be used over an intranet. You do not have to write much of the code and you do not even have to write any special interfaces or classes at the server side. Everything is encapsulated in the following:

System.ServiceModel

System.ServiceModel.Channels

System.ServiceModel.PeerResolvers

Chat Server

This is a very simple chat server with merely four lines of code. As said earlier, you do not need to write any special code. Hence, the server application looks like this.

The code is self explanatory. You create an object of CustomPeerResolverService and pass that object as an input parameter to the ServiceHost class. Open the peer resolver service and host and you are done. No new classes and no end points. Surprising, right? But wait, I have to show you the config file. The config file plays the most important role of specifying the required details.

I have removed '<' and '>' because I was not able to show config files directly. If you know how to show then, please let me know. The config file is very simple. You use a .NET predefined service in this case: System.ServiceModel.PeerResolvers.CustomPeerResolverService . Give the base address for hosting the resolver service as shown. The important thing about the end point is that it uses a System.ServiceModel.PeerResolvers.IPeerResolverContract contract that is already available in the foundation. As said earlier, you use a TCP end point for the communication. Hence, you specify the end point with TCP binding and configure it with security mode as none.

That's it; you are done. Just start the server and you are ready for your chat client.

WCF Chat Application

Chat Client

As compared to the Chat server, the client becomes a little bit complicated. It does everything on its own. As said earlier, you need something common that the server will use to communicate with clients and that can be established by using interfaces. The same concepts apply here also, and you have client code that looks like this.

Here in the config file you have endpoint names "ChatEndpoint", which points to a chat mesh and uses netPeerTcpBinding. When you configure the binding, you use the custom resolver mode and give custom TCP address where your actual server is running. Giving the port number as zero will automatically detect the free port for communication. You use security mode as none. Thus, by using this configuration you can start the clients and send messages across the intranet.

Limitations

I did not find a way to send back a list of online users to the newly joined user. I'll appreciate the solution. Also, let me know if you find anything more interesting. I am also into WCF exploring stage and this might not be the perfect chat application. Any suggestions and corrections are welcome.

Reference

I have referred the Microsoft Technology Samples for getting familiar with WCF and working of different bindings.

Conclusion

You can conclude that WCF provides you a very simple but very powerful way of establishing communication between diverse systems, maintaining the performance benefits with similar OS.

About the Author

Jayant Kulkarni

Dear Friends,
I'm from Pune and curently working with Symantec Corp. I'm having more than 7 years of exp in software field and have worked on areas like ASP.NET, C#, .NET remoting, web services, pocket pc applciations. I'm a brainbench certified software engineer in .NET framework, C#, ADO.NET and ASP.NET.
If you like any of my articles or you want to suggest some changes and improve the way I code, write articles feel free to mail me at: jayantdotnet@gmail.com