WCF Introduction and Contracts

WCF Introduction and Contracts

Introduction

This article demonstrates how to create a WCF service application. This article also covers basic information of all the contracts and a code demonstration.

What is WCF?

WCF is a combined feature of Web Service, Remoting, MSMQ, and COM+. WCF provides a common platform for all .NET communication. It is a part of .Net 3.0.

1.jpg

Difference between WCF and Web service

Web Service WCF
In a web service we need to add the [WebService] attribute to the class. In WCF we need to add the [ServiceContract] attribute to the class.
Add the [WebMethod] attribute to the method in a web service. Add the [OperationContract] attribute to the method in WCF.
For serialization in a web service use the System.Xml.serialization namespace. WCF uses the System.Runtime.Serialization namespace for serialization.
We can host a web service in IIS. We can host WCF in IIS, WAS (Windows Activation Service), self-hosting and a Windows Service.

How to create a WCF service

Let's see how to create a WCF service application step-by-step.

Step 1

Start Menu >> All Programs >> Microsoft Visual Studio 2010 >> Microsoft Visual Studio 2010

In that "File" >> "New" >> "Project..."

2.jpg

Step 2

  • Select WCF from Installed Templates
  • Select .NET Framework 4 from dropdownlist
  • Select WCF Service Application
  • Give the desired name and select the location
  • Click on OK button

3.jpg

Step 3

Now your Windows Communication Foundation service application is ready as a default service. You will see in Solution Explorer Service1.svc and IService1.cs

Open the IService1.cs file, as in:

4.jpg

In this file, you will find a ServiceContract, OperationContract, and DataContract.

Service Contract

Service contract is an attribute applied to an interface i.e. IService1. It describes which operations the client can perform on the services.

5.jpg

Operation Contract

Operation Contract is an attribute applied to methods in interfaces i.e. IService1. It is used to define a method in an interface.

6.jpg

Data Contract

DataContract defines which data types are passed to and from the service. It is used to define a class and the DataMember attribute is used to define the properties.

7.jpg

WCF Contracts map directly to a corresponding web services standard: ServiceContracts map to WSDL DataContracts map to XSD MessageContracts map to SOAP

Step 4

There is one method "GetData" which accepts parameters of type int.

8.jpg

An implementation of this method is in the Service1.svc.cs file. This method returns a string with the value you passed as a parameter.

9.jpg

Step 5

Now let us run this service.

10.jpg

You can test your service in WCF Test Client. WCF Test Client is a GUI tool which enables us to enter parameters, invoke services with these parameters and view the responses returned by services.

Now double-click on the "GetData" method and enter a parameter value of type System.int32. Here I enter "25" as parameter value and press the "Invoke" button. You will get "You entered: 25" as a response.

11.jpg

Conclusion

We have seen a very basic example of a WCF Service application. We entered a parameter value of type int and got the entered value as a response.