Friday, 6 January 2012

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

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 -

1)   Create a new C# windows console application project.
create new console application
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.
Add web service reference
Web service - Adding web service reference
3)   Click on “Advance” button.
Add service reference popup
WCF Service - Add wcf service reference.
4)   Click on “Add Web Reference..” button.
Service reference settings
WCF Service - Reference settings.
5)   An Add Web Reference window will appear. Enter here URL of the web service.
Add web reference popup
WCF Service - Add web reference popup.
Add web reference button
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.
list of web methods
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
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
Web Service - Consumer output.
Your fi­rst 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.

Sharing is Caring »»

11 comments:

  1. Can you make this one better?

    ReplyDelete
  2. I have read so many ρoѕts οn the topic of thе bloggeг lovers ехcept this article is really а pleasant post, keep it up.
    my page - injury lawyer in edmonton AB

    ReplyDelete
  3. Thank you. This article helped me.
    Aaron

    ReplyDelete
  4. Simple and Perfect...Thanks!!

    ReplyDelete
  5. Thanks for Sharing..!! It is very useful professional knowledge.

    Secure Web Design

    ReplyDelete
  6. Nice post. Got a lot of important stuff from this blog.
    dba kings

    ReplyDelete

Follow C# Tutorials