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.
Figure1
Step2
Now click on the Ok Button by
default you will get the following page.
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.
Figure3
Step4
Double-click the GetData() method under IService1.
The GetData tab will be displayed.
Figure4
Step5
In the Request box, select the Value field and
type rohatash.
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.
Figure6
click On the ok Button.
Step7
Add a new form in web application and named it.
Figure7
Step8
Right-click WindowsApplication1 and click Add
Service Reference.
Figure8
Add service reference window will be open.
Figure9
Now copy address from WCF test client window.
Figure10
And paste this address in Add service reference
window.
Figure11
Now click on the OK button.
Now reference have been added in solution
explorer.
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.
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.