C#.Net How To: Consume a WCF Web Service in Visual Studio 2010

In last WCF tutorial we learned what is wcf service? We learned that Windows Communication Foundation (WCF) is a latest technology in web services category. WCF is a often use to develope and deploy network distributed services based on the principles of Service Oriented Architecture (SOA). The fundamental characteristic of WCF is Interoperability. This gives you more manageable approach to create and consume web services. Now, in this C# tutorial i am going to show you how to consume a wcf web service in Visual studio 2010.

I am assuming that your WCF web service is already built and ready to consume in your application. I would suggest you to refer my previous post on how to Create a WCF Web Service in Visual Studio 2010.

Watch Video : How to create and consume a WCF service in console application

Before proceeding with WCF web service consuming process, you must know the URL of your WCF web service. Execute the WCF service you already created and then you will get your WCF web service URL from following screen to consume a wcf web service

WCF Service - URL
WCF Service – URL

Steps to consume a WCF web service in Visual Studio 2010

  1. As described in above page, you will need to run svcutil.exe utility to generate the code files that will be later used in client consumer application. The svcutil.exe utility is installed with the Windows SDK’s and can be found at location – “c:\Program Files\Microsoft SDKs\Windows\vX.0A\bin”, where X can be any latest installed SDK number. In my case the location is –
    “c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin”
  2. Now create a directory/folder to store the proxy files generated by svcutil utility. Here I am creating a folder named “WcfFiles” in c: drive.
  3. Open command prompt, goto c:\WcfFiles location and try following command as given in below image –
    Syntax : c:\Program Files\Microsoft SDKs\Windowsv7.0A\bin\Svcutil.exe your_webservice_url
    WCF Service - SvcUtil.exe
    WCF Service – SvcUtil.exe

    The successful command execution will end with following 2 files in WcfFiles folder.

    1. Service1.cs
    2. Output.config
  4. Now next step is to create a Client or consumer application to consume a wcf web service. Create a new console application –
    New WCF client console app
    New WCF client console app

    Here, I am creating a console application named as “MyFirstWcfWebServiceClientApp” to consume a wcf web service

  5. Goto Add service reference
    WCF - Add reference
    WCF – Add reference
  6. 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 “MyFirstWcfWebServiceReference“.
    WCF service - web method list
    WCF service – web method list

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

  7. Now add your existing Service1.cs file to the project. In step 3 , we have created this file using svcutil.exe utility which is located at “C:\WcfFiles” location.
  8. Add following code to the Program.cs file.
    WCF service - client consume code
    WCF service – client consume code
  9. Now, open app.config file from Solution Explorer
    WCF service - App.Config
    WCF service – App.Config
  10. Remove “MyFirstWcfWebServiceReference” and keep only IService1 in contract.
    WCF service - App.Config endpoints
    WCF service – App.Config endpoints
  11. Save all files and hit F5 button to execute the client application.
    WCF service - Client/Consumer output
    WCF service – Client/Consumer output

Your WCF web service consumer/client application is ready now.

In this article we learn to consume a wcf web service in console application.

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

Related WCF articles …

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.

22 Comments+ Add Comment

  • Good Stuff.. keep it up.

  • One of the best explanations along with the snap shot. I tried it and it works like absolutely fine.
    Great. Thanks a lot

  • Great tutorial, gave me a good start and everything is jus excellent. Thanx!!!

  • HI,

    One doubt

    Can i create proxy without doing adding service reference?

    • Hi,

      Anyone answer please

      scenario :
      1) Having WCF Service
      2) Needs to Access Wcf Service in my client application
      3)The successful command execution of svcutil , will end with 2 files – code file,config file
      4)without doing Add Service Reference

      help plz

    • Yes Suresh, You can generate proxy without adding a service reference. By using SvcUtil.exe command-line utility, you can generate the proxy as shown below –

      SvcUtil http://localhost/MyWCFService/MyWCFService1.svc /out:Proxy.cs

      After creating the Proxy.cs files, add this file to your Client application. Add following reference to your client app

      1) System.ServiceModel
      2) System.RunTime.Serialization

      Add app.config to your client application. Your app.config should contain proper HTTP bindig and your wcf service endpoint URL. If you dont know how to create/modify app.config, just create one sample client application with adding service reference then you will get a app.config file there. Just copy paste the contents of this file to your client application’s app.config.

      This should solve your issue.

  • Thank u so much.
    My new scenario is : orchestration needs to call WCF Service (2 way Service) not using isolated host.

  • Thank u so much.
    My new scenario is : orchestration needs to call WCF Service (2 way Service) not using isolated host.

  • I’m new to all this, so I’m trying to follow the examples closely. I’m using Visual Web Developer Express 2010. I can’t find the console application template used in step 4. I suppose I have a different version. What did you use? Can I use a different template with similar results?

    • Hey thanks for commenting. Yes you can always proceed with some other template, however make sure to code as given above, in that case do not miss step 5 and onwards. For the above article i used VS-2010 Ultimate.
      Please let me know if still you face any issues. Thank you!

  • Nice Articles….easy explanation

  • Superb !
    Simple !

  • good work thank you

  • Nice article indeed.
    One comment on step 10;
    In my case the service reference MyFirstWcfWebServiceReference should not have been removed.

  • Simply Super..
    Thank you

  • Great job bro… keep it up… u made my day…

  • Hi,

    I am getting an error while running WCF saying

    The type ‘MyProject.Service’, provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

    Any help will be appreciated.

  • Excellent Article.

  • Hey Nice Article , I was unknown to write WCF code and uses, but through this I have implemented it. Thankyou.

  • It Works nice

  • best explanation.. keep the good work bro 🙂

  • I loved your tutorial its just simply explained.
    thank you.

Leave a comment