WCF (Windows Communication Foundation) is a powerful framework for building service-oriented applications. It allows developers to create secure, reliable, and transacted web services that can communicate across different platforms and technologies. WCF is part of the .NET Framework and is highly flexible, allowing communication through multiple protocols and formats.
Here’s the ABC of WCF to help you understand its core concepts:
In WCF, an Address is the location where the service is hosted and can be accessed by clients. The address is a URI (Uniform Resource Identifier) that points to the service endpoint.
<service name="CalculatorService">
<endpoint address="http://localhost:8080/Calculator"
binding="basicHttpBinding"
contract="ICalculatorService"/>
</service>
In the above example, http://localhost:8080/Calculator is the Address where the service is hosted.
A Binding in WCF defines the communication mechanisms and protocols used to transmit messages between the client and the service. Bindings specify the transport protocols (e.g., HTTP, TCP), encoding (e.g., text, binary), and security features (e.g., SSL, authentication).
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingConfig">
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
In the above configuration, basicHttpBinding is the Binding used for communication over HTTP.
A Contract in WCF is a formal definition of the operations (methods) that a service exposes. It defines the interface or the contract that the service promises to fulfill. There are two main types of contracts in WCF:
[ServiceContract] and [OperationContract] attributes.[DataContract] and [DataMember] attributes.[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
int Add(int num1, int num2);
}
In the above example, ICalculatorService defines a Service Contract, and the Add method is an operation defined within this contract.
REST (Representational State Transfer) is an architectural style for building web services that operate over HTTP. RESTful services are simpler, lightweight, and more flexible compared to SOAP-based services like those in WCF. REST is widely used for creating APIs for web applications and mobile services.
In RESTful services:
Unlike SOAP (used in WCF), REST does not rely on XML message formats. It typically uses JSON for data exchange, making it more lightweight and easier to integrate with web and mobile clients.
Suppose we have a service that manages books. The service can provide access to books using these HTTP methods:
In a RESTful service, a typical request to retrieve a book might look like:
GET /books/1 HTTP/1.1
Host: api.example.com
The response could return the book details in JSON format:
{
"id": 1,
"title": "Learning REST",
"author": "John Doe",
"year": 2025
}
You can build RESTful services using WCF by using WebHttpBinding, which allows WCF to expose RESTful services over HTTP.
[ServiceContract]
public class BookService
{
[OperationContract]
[WebGet(UriTemplate = "books/{id}")]
public Book GetBook(string id)
{
return new Book { Id = int.Parse(id), Title = "Learning REST", Author = "John Doe" };
}
}
And in the configuration:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingConfig"/>
</webHttpBinding>
</bindings>
<services>
<service name="BookService">
<endpoint address="http://localhost:8080/books"
binding="webHttpBinding"
contract="BookService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
</system.serviceModel>
In this setup, WCF provides a RESTful service using WebHttpBinding. The WebGet attribute defines that the GetBook method can be called via a GET request.
WCF ABC:
Restful Services:
WebHttpBinding.Both WCF and RESTful services are powerful tools for building service-oriented applications, with WCF offering a rich set of features (such as security, transactions, etc.) and REST providing a more lightweight and flexible approach for modern web applications.
Open this section to load past papers