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 https://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.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.PeerResolvers; namespace ChatServer { public partial class ChatServer : Form { private CustomPeerResolverService cprs; private ServiceHost host; public ChatServer() { InitializeComponent(); btnStop.Enabled = false; } private void btnStart_Click(object sender, EventArgs e) { try { cprs = new CustomPeerResolverService(); cprs.RefreshInterval = TimeSpan.FromSeconds(5); host = new ServiceHost(cprs); cprs.ControlShape = true; cprs.Open(); host.Open(TimeSpan.FromDays(1000000)); lblMessage.Text = "Server started successfully."; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { btnStart.Enabled = false; btnStop.Enabled = true; } } private void btnStop_Click(object sender, EventArgs e) { try { cprs.Close(); host.Close(); lblMessage.Text = "Server stopped successfully."; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { btnStart.Enabled = true; btnStop.Enabled = false; } } } }
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.
?xml version="1.0" encoding="utf-8" ? configuration system.serviceModel services service name="System.ServiceModel.PeerResolvers. CustomPeerResolverService" host baseAddresses add baseAddress="net.tcp://10.34.34.241/ChatServer"/ baseAddresses host endpoint address="net.tcp://10.34.34.241/ChatServer" binding="netTcpBinding" bindingConfiguration="TcpConfig" contract="System.ServiceModel.PeerResolvers. IPeerResolverContract" endpoint service services bindings netTcpBinding binding name="TcpConfig" security mode="None"/security binding netTcpBinding bindings system.serviceModel configuration
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.