There is a lot of hype about new (not so new now) technologies from Microsoft and the new architectures that are now a part of Microsoft .Net development environment. These include Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF) and Workflow Foundation (WF).
I will give a brief introduction about what these are just in case you are not familiar. Anyways if you are then probably this article is no good J since I just started working with these. This will help you get a quick start in case you are a starter and I have gathered the info skimming through tutorials and I want to give you something quick and easy. So to start off “What is WPF?” WPF exclusively has everything to do with the UI or user interfaces. Till .Net 2005 the Graphics were GDI (Graphics device Interface) based and in WPF they are Direct X based thus utilizing the faster video card acceleration and fast memory access as compared to the GDI. Secondly this used to be an issue when clients wanted fancy UI e.g. Elliptical buttons, Window Layouts, Animated menus and colors in controls. Microsoft made it possible with user controls to some extent but off course the mainstream developers didn’t want to get in that mess and always wanted to use the default controls with some styling that the client wont like. Customers these days doesn’t want “just another windows button” there should be something innovative about it so WPF enables you to define your own controls with XAML pronounced as (zammel).
The styles, colors, shapes, data bindings and everything that is a part of the control can be done using XAML which is simple xml just defined somewhat like html ( I always see the elements and attributes notations) whenever I see XAML.
So WPF is for UIs and probably an effective solution for at least Windows Vista if not XP. Just as with the “User Interfaces” Microsoft realized that “Services Oriented Architectures” are gaining in popularity. With Central server centric applications and Microsoft efforts to support the SOA with applications like “BizTalk”, “SharePoint” e.t.c something also needed to be done to provide a standard platform for network programming using .Net. Microsoft came with WCF which is a powerful framework for writing and utilizing or consuming web services , windows services , .Net Remoting without needing to write a whole lot of socket interfaces. The implementation is hidden in the components constituting the WCF. WCF like WPF depends heavily on configurations and it is usually a good idea to separate the business logic from the configurations or the (app.config) file. In this article I will demonstrate writing a simple client server program using WCF which will give you some bare essentials for WCF and will help you to explore more in the ever growing world of emerging technologies.
By the way just to get you started if you are not familiar with the client server paradigm, client server application that I am going to develop would be basically two applications. A Server application will let many clients connect to it and say “Hi”. Keeping in mind this is not to tell you how to design better client server applications this is all about getting you up and running with WCF. Ok my friends so don’t get angry =]
Designing the Client and Server with WCF:
Primarily while designing the application remembers there should be something common in the client and server. To explain this better ask yourself the question “How is client going to interact with the Server”? There should be some interface the server should provide to achieve this. If you are familiar with web services think of wsdl (web services description language) that exposes the web service API. So just like that the WCF Service also exposes interfaces. The clients can generate the proxy classes for the API and will call those API methods to communicate with the Server with underlying implementation of Sockets and communication mechanism). I will discuss “How to generate the proxy classes” but first things first “Decide what should the Server be like”? And “what functionality you want to expose”?
So just for the sake of simplicity just think that what the server wants to expose and client wants to consume is 1 single functionality called send message.
The First Step in Writing the Server:
I consider adding the “system.servicemodel” reference a prerequisite to every WCF application so if you have not already added that do make sure the reference is included in the list of your references.
1/ Add a new interface to your solution. I named the solution as WCFServer so the namespace here is WCFServer. I named the interface as CommonInterface just to be clear what this does. This would be the API exposed for the client and would be executed on the server. Good naming convention for this would be to prefix the name of the class with an “I” so something like ICommonInterface but that’s something you know right.
public interface CommonInterface
void SendMessage(string msg);
Observe that there are two things here which might be new to you. The interface is marked as “ServiceContract” which is the attribute which specifies that this is the contract between the client and server.The other thing is the method is marked as “OperationContract”. For the time being just think that every method in this interface should be marked as an “OperationContract”. This method is what will be executed on the server and what the client will call from the “Client code”.
Second Step is to implement the Interface with your server class:
Give a meaning to the interface by implementing it in your server side code.How to do that is declare a class in your program and name it appropriately and inherit it from this interface that we defined in step 1. For our sample application I defined the server class as shown in the definition below:
class Server : CommonInterface
public void SendMessage(string msg)
Console.WriteLine(“Message from client –> “ + msg);
static void Main(string args)
Console.WriteLine(“Starting the server …”);
ServiceHost host = new ServiceHost(typeof(Server));
Console.WriteLine(“Server started successfully at…Pressing a key would cause the server to shut down …”);
So if you look at the code closely you will find the implementation of the “SendMessage” function that will be called by the client and a message would be passed and displayed on the Server’s console window.
If you look at the Main function the first thing that we do is to declare a Service Host and specify the sever’s type. Then use the Open method of the service host to start the service.
Our server at this point is up and running and waiting for the clients to say “Hi”. How to write the clients will be discussed in the next article. Hope this helps getting you a quick start of essentially what it is and if you have any ideas, observations I would love to hear from you in the form of comments.Stay tuned for more and thanks for reading.Happy Coding !!
Here are the Configurations necessary for the configurations:
<?xml version=“1.0“ encoding=“utf-8“?>
<endpoint address=“net.tcp://localhost:5555/Server“ binding=“netTcpBinding“
bindingConfiguration=“” contract=“WCFServer.CommonInterface“ />
You can use the “svcconfigeditor.exe” to generate this so you don’t have to memorize this simply go through a wizard and save the file as app.config and copy and paste.The svccinfigeditor can be found at “C:\Program Files\Microsoft Visual Studio 8\Common7\IDE”