In my previous article Creating WCF Service we learned how to create a simple WCF Service application. This article explains how to consume the WCF Service application in an ASP.Net web Application.
Requirement
You need to keep a WCF service application in running mode so it can be accessible for use, so go to my article Creating WCF Service and create a WCF service and keep it in running mode I hope you have done that.
What does consuming mean?
Many beginners are confused about what means consuming means, but its very simple, it means to use the WCF Services in an application.
Example:
I have created WCF Services and now I want to use it in a real requirement so I used it in an ASP.Net Web Application. In other words, I am consuming the WCF Service in an ASP.Net web application. Similarly, you can use the same WCF Service in Windows, console application, Java and other applications.
I hope you understand the word "consuming".
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New" - "Web..." then in the New WebSite "C#" - "Empty webSite" (to avoid adding a master page).
- Give the project a name, such as "ConsumingWCFservice" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page.
- Then drag three Text Boxes, one Button, and one Label onto the "Default.aspx" page.
Then the <body> section of the Default.aspx page looks as in the following:

In the preceding source code, you have seen that I have taken three text boxes to get input from users because we know that our WCF Service method created as in my article Creating WCF Service takes the three input values day, month and year so I have taken the three text boxes and button click event. We will call the WCF Service method and the output will be displayed on the label control so I have also taken one button and label control.
I hope you understand it.
Now your Solution Explorer of ASP.Net Web Application will be as in the following:

The most important task when consuming a WCF Service in an ASP.Net Web Application is adding the WCF Service reference into the ASP.Net web application. So how to add it? Let us see the procedure.
1. Right-click on the ASP.Net Web Application and click on "Add Service Reference" as in the following:


- Run the WCF Service we created in my article Creating WCF Service by clicking on F5 or whatever other option you are familiar with, it will then show the following WFC Client window and copy the address as

After pasting the URL in the preceding window box, click on the green right headed arrow button, it will discover the WCF Services available related to that URL address and you see that in that related URL one WCF Service is found message is displayed along with the WCF Service name, "WCF Services" in the preceding right hand side window.
- WCF Service Reference Name
In the right hand corner of the window you have seen the option for the Web reference name; the web reference name is anything you wish and this name will be added in your ASP.Net Web Application as allies name for Web Service. In my article, I have given the web reference name as "ServiceReference".
Then after adding the WCF Service reference in the ASP.Net web application the Solution Explorer will look as in the following:



Chulan KumaraPosted May 17, 2019, 1:55 AM
Fantastic simple tutorial that anyone can understand. I was stuck at consuming the created wcf service and was trying to host on iis, run as windows service etc. This really saved a lot of my precious time and aided in my task I was assigned.