Jan
6
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? Consuming a .net web service is not a big task. The screenshots along with each steps would help you understand the how to consume a web service in c#.net.

Watch Video: How to create and consume asp.net web service!

Steps to consume a web service in c#.net?

  1. Create a new C# console application project.
    C# - Creating Console Application
    C# – Creating Console Application

    Here, I have specified name of the project as “MyFirstWebServiceConsumerApp“. Click on Ok button to proceed further.

  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.
    C# - Adding Web Service Reference
    C# – Adding Web Service Reference
  3. Click on “Advance” button.
    C# - Web Service Reference
    C# -Web Service Reference
  4. Click on “Add Web Reference..” button.
    C# - Web Service Reference Settings
    C# -Web Service Reference Settings
  5. An Add Web Reference window will appear to consume a web service in c#.net. Enter here URL of the web service.
    C# - Consume a web service in c#.net
    C# -Web Service URL

    Now, click on button(See below image) to retrieve all web service methods in this window.

    C# - Add Web Service Reference button
    C# – Add 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 would show you the available Web Methods and the status will appear as “1 Service Found:
    C# - List of Web Methods
    C# – List of Web Methods
  7. Enter web service reference name. I used “MyFirstWebServiceReference” as a web service reference name as shown in above image. Now, click on “Add Reference” button.
  8. Now check the Solution Explorer. “MyFirstWebServiceReference” must get added under web reference folder.
    C# - Web Service Reference in Solution Explorer
    C# – Web Service Reference in Solution Explorer
  9. The next step is to add Reference of this web service to our c# code, so that we can use this in code files. Add following line in code file consume a web service in c#.net –
           using MyFirstWebServiceConsumerApp.MyFirstWebServiceReference;
  10. Add below code to call the web method. So 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();
            }
        }
    }
  11. Now save the files and Execute the application. Press F5 button on your keyboard to execute the code. The result should appear as below screenshot –
    C# - consume a web service in C#.net console application
    C# – Web Service output in Console Application

Your fi­rst web service consumer console application is ready.

Sharing is caring..!! If you like the article then could you please share the article on social media?

More articles for you…

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.

25 Comments+ Add Comment

  • ,/

  • Can you make this one better?

  • Nice article

  • 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 pageinjury lawyer in edmonton AB

  • Thanks, nice article!

  • This helped my a lot

  • Thank you. This article helped me.
    Aaron

  • Simple and Perfect…Thanks!!

  • Thanks for Sharing..!! It is very useful professional knowledge.

    Secure Web Design

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

  • Thx, good!

  • Nice post very helpful

    dbakings

  • very helpfull for begineers

  • can the address of the web service be configured at run time?
    I ask because at customer sites they will not be the same url/uri.

  • Very nice example for web service.
    Thank you for sharing such a nice post.
    god bless you.

    Thanks
    Manu

    Kids Mitra
    http://www.kidsmitra.com

  • Nice Understood, Great work :)

  • Helpful article. Thanks a lot.

  • Thanks you Dude!!

  • Excellent!

  • The guidance is reli nice,everyone can develop their own web service in just a few minutes.

  • Gud one friend:)

  • Thanks a lot.. It helped to connect to lotus notes from sharepoint

  • Can you provide a code of a web service that calls its own URL to get the data pushed to its URL? Thanks

  • Thank you very much. I was looking for an example like this one.
    I have another web service reference: http://www.webservicex.net/global weather.asmx?WSDL
    An you can use the method GetWeather(“city name”,”country name”).

    You saved me!

  • !Very good!!! I will add this site to my favorites sites :)

Leave a comment