Windows Communication Foundation (WCF) is an integral part of .NET Framework 3.*. “It provides a unified framework for rapidly building service-oriented applications that makes it easy to build and consume secure, reliable, and transacted Web services. It unifies the capabilities in ASMX, WSE, Remoting, COM+, and MSMQ; therefore developers need to learn only one programming model.” -MSDN
And Today we are going to see a very very basic example of using Windows Communication Foundation(WCF). So let’s begin.
Pre-Requisites:
The first thing you need is .NET framework 3.*, 3.5 SP1 in particular 🙂 which you can download from Microsoft Download Center. Okay, the next thing is IDE. If you don’t have Visual Studio, there are express edition(s) available that are free to download and use. In particular, I’ve just downloaded Visual C# 2008 Express Edition which seems cool 🙂
Get Set Go!
Service
First of all we need to create a Service and for that I’m going to create a new project i.e. Class Library with name RegistrationService as shown below:
Now we need to add reference of System.ServiceModel which is main dll required for WCF.
Next we need to create a contract of Service i.e. the interface of the service having required attributes as shown below:
[ServiceContract(Namespace="http://adil.com/RegistrationService/2009")]
public interface IRegistrationService
{
[OperationContract]
string RegisterName(string name);
}
Notice that the interface is having ServiceContractAttribute which is mandatory and indicates that an interface defines a service contract in a WCF application. The namespace parameter is optional and is used to define unique namespace to the service and by default its set to http://tempuri.org/. Also notice that OperationContractAttribute is marked on the method(s) need to be exposed to the client.
Next we are going to add a class with name RegistrationService.cs which will implement this interface.
public class RegistrationService: IRegistrationService
{
public string RegisterName(string name)
{
return string.Format("Registered Name : " + name);
}
}
All right, till this point our service is ready. We have contract to be exposed and we have class implementing that contract. Now we need to have a Host Application (Executable). Every service need to be hosted either on IIS or WinApp or Window Service etc. So, for this example, we will create a Console Application to Host our Registration Service.
Also you need to add System.ServiceModel reference to this host (executable) application as well. The Host application need to define the ABC of WCF which is
- A-> Address (Address where service will be hosted)
- B-> Binding (For now, take it as underlying protocol like HTTP, TCP etc)
- C-> Contract (The interface of the service)
We will create a class Host for hosting this RegistrationService.
public class Host
{
ServiceHost host;
public void StartService()
{
try
{
host = new ServiceHost(typeof(RegistrationService));
host.AddServiceEndpoint(typeof(IRegistrationService),
new BasicHttpBinding(),
"http://localhost:8000/RegistrationService");
host.Open();
Console.WriteLine("Service Started Successfully");
Console.ReadLine();
}
catch (System.Exception exception)
{
Console.WriteLine("Unable to start service n " + exception.Message);
}
}
public void StopService()
{
if (host != null)
host.Close();
}
}
Notice that the line
host.AddServiceEndpoint(typeof(IRegistrationService),
new BasicHttpBinding(),
"http://localhost:8000/RegistrationService");
basically adds an Endpoint where service will be hosted defining the type of contract i.e. IRegistrationService, the underlying protocal i.e. BasicHttpBinding and the Address. Let’s hit F5 and run it.
That is all for the service.
Client
The client need to have proxy of the service, in general, ABC of the service. For proxy we will copy the contract of the service at the client side and create proxy using ChannelFactory class of WCF. Further the type of contract, Binding and Endpoint Address is passed to ChannelFactory constructor. Consider the code snippet below:
[ServiceContract(Namespace = "http://adil.com/RegistrationService/2009")]
public interface IRegistrationService
{
[OperationContract]
string RegisterName(string name);
}
class Program
{
static void Main(string[] args)
{
IRegistrationService proxy = ChannelFactory.CreateChannel
(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/RegistrationService"));
Console.WriteLine("nCalling Service...n");
Console.WriteLine(proxy.RegisterName("Adil"));
Console.ReadLine();
}
}
That’s it. Let hit F5,
So we have created a simple client/server application using WCF. Please note that this is very simple example in which both client and server know the address, contract and binding. In real scenario, the service information is exposed to client using WSDL (Web Services description Language).
5 Comments
Muhammad Ammad Saleem · February 16, 2009 at 5:55 am
Really nice effort Adil Keep it up. The article is brief but contains all the information any one will need. This art of being precise and brief is not mastered easily, so keep it up. Thumbs for the Post, the Blog and Adil
Adil Mughal · February 16, 2009 at 7:24 am
Thank you Ammad for your appreciation. I will definitely try my best to disseminate information and serve the community in future.
Ali Raza · February 19, 2009 at 5:01 am
Impressive work adil
Hadi Raza · February 19, 2009 at 2:42 pm
Nice effort! I was not much aware with WCF,the blog gives me a good brief information about it. U can make it more helpful for new ones by adding some past scenario’s info. and by defining some keywords like Service,Binding,Contract,Behaviors
End Points etc.
Good work! keep it up.
Anonymous · February 2, 2012 at 10:18 am
So much thankful for the nice Article on WCF, intact with the basic.