This c# tutorial explains how to consume a web service in a C# console application program. I am assuming you have already created a c# web service. Please refer my previous post on how to create a web service application and how to create simple wcf service? creating a .net web service is not a big task.
Lets follow the steps to create c# web service consumer application -
Lets follow the steps to create c# web service consumer application -
1) Create a new C# windows console application project.
![]() |
| web service - create new console application |
Here, I am giving name of the project as “MyFirstWebServiceConsumerApp”. Click on Ok button.
2) Go to Solution Explorer and right click on your Console Application Project Name. In this case, right click on “MyFirstWebServiceConsumerApp” and select “Add Service Reference…” from the drop down menu.
![]() |
| Web service - Adding web service reference |
3) Click on “Advance” button.
|
| WCF Service - Add wcf service reference. |
4) Click on “Add Web Reference..” button.
|
| WCF Service - Reference settings. |
5) An Add Web Reference window will appear. Enter here URL of the web service.
|
| WCF Service - Add web reference popup. |
![]() |
| Check Web Service Reference Button |
6) Enter the Web Service URL and click on -> button to check whether the Web Service URL is valid or not. If the URL is valid, it will show you the available Web Methods and the status will appear as “1 Service Found:”
Enter web service reference name. I used “MyFirstWebServiceReference” as a web service reference name.
|
| Web Service - List of all available web methods. |
Now, click on “Add Reference” button.
7) Now check the Solution Explorer. “MyFirstWebServiceReference” should be added under web reference folder.
|
| Web Service - Solution explorer. |
8) The next step is to add Reference to our c# code. Add following line
using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;
9) Add code to call the web method. The final code will appear as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;
namespace MyFirstWebServiceConsumerApp
{
class Program
{
static void Main(string[] args)
{
Service1 webService = new Service1();
Console.WriteLine(webService.MyFirstWebMethod("Bradd", "Pitt"));
Console.ReadLine();
}
}
}
10) Now hit F5 button on keyboard to execute the code. The result should appear as
|
| Web Service - Consumer output. |
Your first web service consumer console application is ready.
Please read more on how to create, host and consume wcf service.
You may also want to check the complete list of Asp.net tutorials.
If you liked the article then could you please share the article on social media? You can use below sharing is Caring widget to share.
Please read more on how to create, host and consume wcf service.
You may also want to check the complete list of Asp.net tutorials.
If you liked the article then could you please share the article on social media? You can use below sharing is Caring widget to share.
Sharing is Caring »»
|
|
|
Tweet |









,/
ReplyDeleteCan you make this one better?
ReplyDeleteNice article
ReplyDeleteI have read so many ρoѕts οn the topic of thе bloggeг lovers ехcept this article is really а pleasant post, keep it up.
ReplyDeletemy page - injury lawyer in edmonton AB
Thanks, nice article!
ReplyDeleteThis helped my a lot
ReplyDeleteThank you. This article helped me.
ReplyDeleteAaron
Simple and Perfect...Thanks!!
ReplyDeleteThanks for Sharing..!! It is very useful professional knowledge.
ReplyDeleteSecure Web Design
Nice post. Got a lot of important stuff from this blog.
ReplyDeletedba kings
Thx, good!
ReplyDelete