What is WCF? How to create WCF in ASP.NET?

In this article I am going to explain about what is WCF and how to create WCF in ASP.NET basic example program. Through this article learn about WCF basic and integrate WCF services in ASP.NET .

What is WCF?

WCF expansion is Windows communication Foundation, It is a service oriented technology. WCF is a advanced future of Web service. It is created with four major features i.e. WCF is combined with Web service, Remoting, MSMQ and COMT concept.

Advantage of WCF:

1) Compared to Web service WCF support more protocols like TCP/IP, PIPE, MSMQ, HTTP. No need to write separate code for each protocol just adding endpoint for each protocol it's enough.

2) WCF is more secured and reliable compare to web service.

3) Service oriented technology

4) WCF can be hosted on IIS, WAS, Self hosting, Windows services too.

What is Endpoint in WCF?

EndPoint is denoted as how to communicate with WCF service in ASP.NET. It mean which protocol are used to access WCF service etc. If user want access WCF service is more that protocol then add those protocols and details in the web.config file endpoint under services tag.

There are three main components are used in the EndPoint

1) Address :
It is used to denoted where our WCF service is hosted, user can use this URL to call our web service.

2) Binding:
It is used to denote how to access WCF service with which protocol HTTP or TCP-IP etc. are used to call WCF Service

3) Contract:
Contract is used to mention WCF Interface name to user know about that.

How to create first WCF application?

First open Visual studio and Choose File -> Website -> WCF Service. After you create it two classes are created in APP_Code Folder

1) IMyservice.CS

2) MyService.cs

Interface Code
Write below code in IMyservice.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
public interface IMyService
string HelloWorld(string name);


What is ServiceContract?
Service contracts is defined as a operation that service can provide. Service contract created with interface.

What is OperationContract?
OperationContract is used to defined which methods are performed in the WCF etc. In this example I am going to use HelloWorld method and return as string.

Interface Implemented Class Code
Write below code in Myservice.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

public class MyService : IMyService

#region IMyService Members

public string HelloWorld(string name)
return "Hello " + name;


In the above code block I have implemented that interface method. Now going to configure End point, service nameetc. and web.config file like below

Set Endpoint detail correctly in Web.config


In the above image I clearly mention where I hosted my WCF service like IIS, and end point is wsHTTpBinding Contract is IMyService

Note: If you change contract name in the interface then need to change here also then only working

After that run WCF Services the output page is look like this

Now create one new ASP.NET website File -> New website

Right click on the project name in the solution explorer and choose "Add Web Reference" then put above WCF service endpoint URL to call WCF service method and variables.

Client side

I have placed one text box control and button to test WCF service

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
Enter User name <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Test WCF Service"
onclick="Button1_Click" /><br />
<asp:Label ID="lblResult" runat="server"></asp:Label>


Server side

In server side I have create instance for WCF Service class and access the HelloWorld method

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
lblResult.Text = "";
protected void Button1_Click(object sender, EventArgs e)
//Create instance for WCF Service class
WCFRef.MyServiceClient obj = new WCFRef.MyServiceClient();
//Using instance call WCF service method and get back return string bind in the label
lblResult.Text = obj.HelloWorld(TextBox1.Text);



Source code:

Client Side: ASP.NET
Code Behind: C#


I hope this code snippet is help you to know about basic of WCF Service.


  • WCF_Service_Source (43963-72814-WCF-Service-Source.rar)
  • Comments

    Author: SonyShiva24 Jun 2012 Member Level: Gold   Points : 0

    Hi Ravindran,

    How we can say WCF is secured one in comparision with Web Services/ any?
    OR How we can give security to WCF Application?

    Guest Author: 18 Jan 2013

    Hopw can learn WCF?

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: