WCF Introduction and Contracts - Day 1

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.

What-is-WCF.jpg

Difference between WCF and Web service

  • 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 .

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..."

Microsoft-Visual-Studio-2010.jpg

Step 2

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

    Select-WCF-Installed-Templates.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:

Open-Service1.cs-file.jpg

In this file you willl 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.

Service-Contract.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.

Operation-Contract.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.

Data-Contract.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.

WCF-Contracts-map.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.

Service1-svc-cs.jpg

Step 5

Now let us run this service.

run-this-service.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 a parameter value and press the "Invoke" button. You will get "You entered: 25" as a response.

GetData-method.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.
 

Up Next
    Ebook Download
    View all
    Learn
    View all