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|
Steps to consume a WCF web service in Visual Studio 2010
- 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”
- 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.
- 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
The successful command execution will end with following 2 files in WcfFiles folder.
- 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
Here, I am creating a console application named as “MyFirstWcfWebServiceClientApp” to consume a wcf web service
- Goto Add service reference
WCF – Add reference
- 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
Click on Ok button to finish the Add service reference wizard.
- 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.
- Add following code to the Program.cs file.
WCF service – client consume code
- Now, open app.config file from Solution Explorer
WCF service – App.Config
- Remove “MyFirstWcfWebServiceReference” and keep only IService1 in contract.
WCF service – App.Config endpoints
- Save all files and hit F5 button to execute the client application.
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 …
- How to Create WCF service.
- How to host wcf service in IIS.
- How to consume wcf service in Asp.net Application.
- Difference between Web Service and WCF.
- How to consume a WCF service in WPF application.
- More WCF articles…