Wednesday, 1 August 2012

Filled Under:
, ,

C#.net How to: Consume a WCF service in ASP.net Website application

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..

In this c# tutorial, we would learn to consume a WCF service in Asp.net application. Calling a wcf in asp.net is not a big task. Before we start, i am assuming that your WCF web 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

Steps to consume WCF service in Asp.net Application

  1. As we are going to consume a WCF service in Asp.net web application, the first step is to create one Asp.net web application which would then act as a Client/Consumer for wcf service. To create a asp.net web application open VS 2010, goto menu File -> New -> Web Site...
    WCF in Asp.net Application
    Creating Web Site Application
  2. Here, specifying name of new web application as "MyFirstWcfWebServiceClientWebApp"
    WCF in Asp.net Application
    Creating Web Site Application
  3. If ASP.net 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 Asp.net
    WCF Reference in Asp.net
  5. 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.
    Asp.net Default code
    Asp.net Default code
  8. Go to toolbox and add label control to the Defaults.aspx page.
    Adding label control in Asp.net
    Adding label control in Asp.net
  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 Asp.net
    WCF calling code in Asp.net
    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 –
    WCF consumed in Asp.net
    WCF consumed in Asp.net
In this wcf tutorial we learned to consume a WCF Service in asp.net web application. This is a perfect wcf example in asp.net.

I hope you enjoyed this asp.net 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 -

Protected by Copyscape Plagiarism Check

Sharing is Caring...





7 comments:

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

    ReplyDelete
  2. Just what I was looking for. Thanks for the post :)
    - http://testing-qna.blogspot.in

    ReplyDelete
  3. Really very use full...Your effort in doing this is appreciable..

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

    ReplyDelete