In this article I am going to discuss how to create a WCF Service and use in asp.net.

How to Create a WCF service

Step 1

To create new WCF Service Open new web site and select WCF service like as below.

wcf1.gif

Figure1

Step2

Now click on the Ok Button by default you will get the following page.

wcf2.gif

Figure2

Iservice1.cs

Change the type for the value parameter to String.

Such as:

[OperationContract]
string GetData(string value);

Service1.svc.cs

Change the type for the value parameter to String.

Such as:

public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}

Testing the Service

Step3

Press F5 to run the service. A WCF Test Client form will be displayed and it will load the service.

wcf3.gif

Figure3

Step4

Double-click the GetData() method under IService1. The GetData tab will be displayed.

wcf4.gif

Figure4

Step5

In the Request box, select the Value field and type rohatash.

wcf5.gif

Figure5

Accessing the Service

Step-6

In the New Project dialog box, expand the Visual C# node and select web and then select  Application. Click OK to open the project.

wcf6.gif

Figure6

click On the ok Button.

Step7

Add a new form in web application and named it.

wcf7.gif

Figure7

Step8

Right-click WindowsApplication1 and click Add Service Reference.

wcf8.gif

Figure8

Add service reference window will be open.

wcf9.gif

Figure9

Now copy address from WCF test client window.

wcf10.gif

Figure10

And paste this address in Add service reference window.

wcf11.gif

Figure11

Now click on the OK button.

Now reference have been added in solution explorer.

wcf12.gif

Figure12

To build a client application

Step9

In Solution Explorer, double-click webservices.aspx to open the web Form in designing window. Add one TextBox, Label and Button control on the form.

The Form looks like below.

wcf13.gif

Figure13

Step10

Double-click the Button, and add the following code in the Click event handler.

protected void Button1_Click(object sender, EventArgs e)

        {

            ServiceReference1.Service1Client client = new

            ServiceReference1.Service1Client();

            string returnString;

            returnString = client.GetData(TextBox1.Text);

            Label1.Text = returnString;

        }

 

Now run the application.

Press F5 to run the project. Enter some text and click the button. The label will display "You entered:" and the text that you entered.

wcf14.gif

Next Recommended Readings