What is Windows Communication Foundation (W.C.F.)?
Over the years, Microsoft has introduced new and different ways of implementing distributed systems from DCOM to .NET Remoting to Webservices. With .NET 3.0, comes Windows Communication Foundation (WCF) which is meant to improve on all that has gone before. Most of the older technologies such as .NET Remoting meant the application could only communicate with other .NET services and not for example JAVA. WCF is meant to resolve inter-compatibility errors so you can develop an application on one machine and not worry about what architecture the remote system is developed in.
To develop a WCF, you need either MS Visual Developer or Visual Studio. If like me you're using the Express edition, you'll need the Visual Developer application. In Express, you select New Website then WCF Service not a project. It should create a blank project with just a few items. If there is App_Data but no App_Code or when you view the code, there is a Namespace, you've gone wrong and need to start again.
Open up the IService file, change the IService interface if you so wish like how I've done in the example. The class must be prefixed by the [ServiceContract] definition as you can see in the example for it to be accessible by the calling service.
[OperationContracts] are functions and methods that can be called by the remote site. Any classes that you wish to return, you define as [DataContracts]. Our interfact class will eventually look like the following :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// ServiceModel is the main class when using WCF, it must be included every time.
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IPlanet
{
[OperationContract]
List
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class PlanetDetails
{
public PlanetDetails(string pName, string pMeaning, int pPosition, int pMoonCount)
{
PlanetName = pName;
Meaning = pMeaning;
Position = pPosition;
MoonCount = pMoonCount;
}
[DataMember]
public string PlanetName { get; set; }
[DataMember]
public string Meaning { get; set; }
[DataMember]
public int Position { get; set; }
[DataMember]
public int MoonCount { get; set; }
}
You should now define the main class that will provide the functionality for the Service. Using interfaces isn't mandatory but it helps. All the [] directives must be on the interface rather than the class but if not using interfacing, the directives should be on the main class. Our solar system class which uses the interface looks like the following. Before you write in and say there's only eight planets, I don't care for this example.
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class SolarSystem : IPlanet
{
public List
{
List
Planets.Add(new PlanetDetails("Mercury", "The Messenger", 1, 0));
Planets.Add(new PlanetDetails("Venus", "Goddess of Love", 2, 0));
Planets.Add(new PlanetDetails("Earth", "Unknown", 3, 1));
Planets.Add(new PlanetDetails("Mars", "God of War", 4, 2));
Planets.Add(new PlanetDetails("Jupiter", "King of Gods", 5, 16));
Planets.Add(new PlanetDetails("Saturn", "God of Time", 6, 18));
Planets.Add(new PlanetDetails("Uranus", "Father of Saturn", 7, 15));
Planets.Add(new PlanetDetails("Neptune", "God of Sea", 8, 8));
Planets.Add(new PlanetDetails("Pluto", "God of the Underworld", 9, 4));
return Planets;
}
}
When as I have done here, renamed the class from Service to SolarSystem, you need to update the Service value in the .svc file otherwise you will get errors.
The next stage is to write a C# program to access the WCF service so you'll need Microsoft C# Developer next. Choose either a WPF or a Winforms application, for our example, I am going to use a console application. Once you have created a basic application, the first thing to do is to add a Service Reference.To do this, right click on the project explorer and add service.
Give the Namespace a decent name, the name is different from the application namespace to avoid errors. Then code the application in the same way as you would any other. For our application, I did the following code :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SolarSystem
{
class Program
{
static void Main(string[] args)
{
ThePlanets.PlanetClient OurPlanets = new ThePlanets.PlanetClient("EightPlanets");
List
foreach (ThePlanets.PlanetDetails pl in PlanetList)
{
Console.WriteLine("Planet {0}: - {1}",pl.Position,pl.PlanetName);
}
Console.ReadLine();
}
}
}
You will see in the above example that I've put EightPlanets as a parameter in the function declaration, this is the EndPoint/Remote Site that the application will communicate with. Endpoints are declared in the app.config file and details the addresss of the sites it can connect to.
<endpoint address="http://misterdeveloper.com/EightPlanets/Service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPlanet"
contract="ThePlanets.IPlanet" name="EightPlanets" />
You can if you like use the above use the above WCF service to test. If you add another EndPoint calling the new one Nine instead of Eight and changing the address, you can experiment with connecting to different services without needing to recompile the code. Both WCF services only provide one function and that is to return a list of planets, you can't add, delete or update the data.
The example that I have shown does not connect to a database and doesn't receive and parameters but that does not mean WCF service functions can't accept them, its just I've kept it simple.
Tags - .NET
Last Modified : June 2023