Windows Communication Foundation (WCF)
More Insights
White Papers
- Transitioning to Multicore Development
- Crime Prediction and Prevention: A Safer Public through Advanced Analytics
Reports
More >>Webcasts
More >>The Windows Communication Foundation (WCF) was introduced as part of .NET Framework 3.0. WCF is a Microsoft framework that you can use to implement connected, service-oriented, reliable, transacted services that are reliable and secure. In WCF you have a framework that provides an excellent unification of Microsoft's distributing technologies (Web Services, Remoting, COM+, and so on) under a single umbrella.
The three main concepts related to WCF are:
- Address
- Binding
- Contract
While address denotes the location of the service, binding specifies the communication protocol and the security mechanisms that apply. A contract defines the parts of the service that is exposed. In WCF, a client connects to a service using endpoints exposed by the service. An endpoint contains the address that denotes where the endpoint can be accessed and the binding information for the endpoint. When a client needs to connect to a service and execute one or more of its exposed methods, it should adhere to the binding specified by the endpoint.
RESTful Web Services
A RESTful Web Service (or the RESTful Web API) is a service that comprises of a collection of resources. These resources include a base URI that is used to access the web service, a MIME type (i.e., JSON, XML, etc) and a set of defined operations (i.e., POST, GET, PUT or DELETE).
It should be noted that similar to a Web Service, a RESTful Service is platform and language neutral. However, unlike a Web Service, there isn't any official standard set for RESTful Services -- it's simply an architectural style.
Implementing a RESTful Service using WCF
To implement a RESTful Service using WCF, you start by using Visual Studio 2010 to create a WCF service and then make the service RESTful using the necessary attributes. To give you an idea of what the Solution Explorer would look like when implementing the Service, Figure 1 shows the completed Service Host and the Service Client.
Creating the WCF Service
A WCF service is comprised of the following:
- Service class
- Service contract
- Hosting environment
- One or more endpoints
The Service class in WCF implements a Service contract. The Service contract is in turn decorated using the [ServiceContract] attribute. A WCF Service needs to be hosted in a hosting environment--you can have your WCF Service hosted in IIS, or make it a self-hosted service. The hosting environment denotes the environment in which WCF Service executes. WCF Clients use the endpoints to access the WCF Service. A WCF Service contains one or more methods that are exposed to the outer world using the [OperationContract] attribute.
To create a WCF service using Visual Studio 2010, follow these steps:
- Open Visual Studio 2010 IDE.
- Click on File -> New -> Project.
- Select WCF Service Application from the list of the project templates displayed
- Specify a name for the WCF Service Application and click OK to save.
This would create a WCF Service Application project. Here's what the Service class looks like:
using System;
namespace DDJService
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
A Service class in WCF implements a Service Contract -- an interface that contains the [ServiceContract] attribute. Here's what this interface looks like:
using System.Runtime.Serialization;
using System.ServiceModel;
namespace DDJService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
Implementing the Service Contract
I will now implement a WCF Service Contract called "TestService" for our RESTful Web Service example. I'll make the service RESTful later; for now, I focus on creating a Service Contract.
So what is a Service Contract, anyway? A Service Contract is an interface that is marked with the
[ServiceContract]
public interface ITestService
{
[OperationContract]
Employee GetEmployee(Int32 employeeID);
Employee PostEmployee(Int32 employeeID);
}
The GetEmployee and PostEmployee methods return instances of the Employee class. The Employee class is a custom class -- a complex type that is marked with the [DataContract] attribute so that instances of this class can be serialized. Note that you can only transfer instances that are serialized -- the primitive types in .NET are all serialized by default. The Employee class is shown below:
[DataContract(Namespace = "")]
public class Employee
{
[DataMember]
public Int32 EmployeeID { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public String Address { get; set; }
}



