Consuming WCF service in asp.net application

In this article we will see how to consume WCF service in Asp.net application step by step.


1. Right click on the Asp.net application References and select Add Service Reference

add service reference

2. In the Add Service Reference dialogue box, Click on Discover and Select Services in Solution.

discover service

3. Give Namespace as WCFServiceReference and click on OK.

service reference

4. WCF service reference will be add to solution under Service References.

service reference in solution

5. Bindings and endpoint will be added to web.config as shown below.


6. Add a button & lable in ConsumeWCF.aspx




7. Implement btnGetTime_Click method in ConsumeWCF.aspx.cs

protected void btnGetTime_Click(object sender, EventArgs e) { WCFServiceReference.Service1Client objWCF=new WCFServiceReference.Service1Client(); lblTime.Text = objWCF.GetDateTime(); }

Where WCFServiceReference is service reference and created service client object objWCF. Now we can call the GetDateTime() method of WCF Service like objWCF.GetDateTime();


Output:

consumed service output

Download sample project : Download