Consume a Web Service in C#.Net
C#.Net How To: Consume a Web Service in C#.Net - Consume WebService in Windows Console Application
C#.Net How To: Consume a Web Service in C#.Net - Consume WebService in Windows Console Application
The Tutorial explains how to consume a web service in a C# console application program. I am assuming you have already created a web service. Please refer my previous post onhow to create a web service application.
1) Create a new C# windows console application project.
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.
3) Click on “Advance” button.
4) Click on “Add Web Reference..” button.
5) An Add Web Reference window will appear. Enter here URL of the web service.
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.
Now, click on “Add Reference” button.
7) Now check the Solution Explorer. “MyFirstWebServiceReference” should be added under web reference folder.
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
Your first web service consumer console application is ready.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home