2012 // C# // WCF How to: Consume a WCF service in Website application

In this c# tutorial, we would learn to consume a WCF service in application. Calling a wcf in is not a big task. Before we start, i am assuming that your WCF service is already built and ready to consume. If it’s not then i would suggest to read my article how to create a wcf service. You must know the URL of your WCF web service. Execute the wcf service you created and then you will get your WCF web service URL from following screen –

WCF Service URL
WCF Service URL

In previous wcf tutorials, we have seeen how to create a wcf service and various wcf hosting articles like how to host wcf service in IIS, Windows Service and how to self-host a wcf service. There are again many ways to consume WCF service like – consuming a wcf service in console application, cosuming a wcf service hosted as windows service, consuming a wcf service in WPF application, etc..

Steps to consume a WCF service in Application

Follow below steps of this wcf example in

  1. As we are going to consume a WCF service in web application, the first step is to create one web application which would then act as a Client/Consumer for wcf service. To create a web application open VS 2010, goto menu File-> New -> Web Site…
    Consume a WCF service in
    Creating Web Site Application
  2. Here, specify name of new web application as “MyFirstWcfWebServiceClientWebApp
    WCF in Application
    Creating Web Site Application
  3. If 4.0 web extension not currently enabled on the Web server, you may get following error. Click on Yes button to enable ASP.Net 4.0 on the web server. If it is already enabled then you wont get this error and in this case skip this step.
    Web extension error
    Web extension error
  4. Now let’s move our focus on adding WCF service in this web application. Right click on project name and select “Add Service Reference…” from context menu.
    WCF Reference in
    WCF Reference in
  5. To consume a wcf service in , enter your WCF web service URL and click on Go button to see a list of available services. All your available web methods (services) will appear on the pane below. Give specific reference namespace. Here I am giving reference namespace as “MyFirstWcfServiceReference“.
    List of WCF services
    List of WCF services

    Click on Ok button to finish the Add service reference Wizard.

  6. Now, observe solution Explorer, you will find MyFirstWCFServiceReference is added in solution.
    WCF reference in Solution Explorer
    WCF reference in Solution Explorer
  7. Now, it’s time to move our focus again in Web application. Open Defaults.aspx file, remove “Welcome to ASP.NET!” contents and add a label instead to show the return message coming from the WCF service. Default code Default code
  8. Go to toolbox and add label control to the Defaults.aspx page.
    Adding label control in
    Adding label control in
  9. Open Defaults.aspx.cs file and add following code in Page_Load event as shown in below image. The code is for calling a wcf service in Page_Load event of a page.
    protected void Page_Load(object sender, EventArgs e)
        MyFirstWCFServiceReference.ServiceClient wcfServiceClient = new MyFirstWCFServiceReference.ServiceClient();
        Label1.Text = wcfServiceClient.WelcomeMessage(“Ashish”);

    WCF code in
    WCF calling code in

    Save all the files. Hurray!!! your wcf service consumer web application is ready now.

  10. Now execute your web appliation to get the result. Press F5 and you will get output in your default web browser as below –
    Consume a wcf service in
    WCF consumed in

In this wcf tutorial we learned to consume a WCF Service in web application.

I hope you enjoyed this wcf article. If you find this article helpful, then could you please share the article on your social media?

Following few articles might be of your interest –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

9 Comments+ Add Comment

  • it is showing error on MyFirstWCFServiceReference in Defaults.aspx.cs file.

    • Can you paste here the error details?

    • good

    • good

  • Just what I was looking for. Thanks for the post 🙂

  • Really very use full…Your effort in doing this is appreciable..

  • really good ,and its very useful 4 beginners..

  • Gr8 explanation
    thanks a lot 🙂

  • nice

Leave a comment