Click here to Skip to main content
13,866,422 members
Click here to Skip to main content
Add your own
alternative version


105 bookmarked
Posted 14 Aug 2002

Consuming Web Services from a Win Forms Application

, 14 Aug 2002
Rate this:
Please Sign up or sign in to vote.
How to access and use a web service from a C# application, using the CodeProject Web Services.

Sample Image - cpwebserviceconsumer.jpg



v. con·sumed, con·sum·ing, con·sumes
v. tr.
      a.  To expend; use up.
      b.  To purchase (goods or services) for direct use or ownership.
   5.  To absorb; engross.

Using an XML Web Service is a lot like going to a restaurant, ordering some food and eating or "consuming" it. For this article, I have been seated at the bar of the CodeProject Cafe and given a menu. I tell Chris (the bartender) that I want a beer and a list of the latest articles, and in seconds he puts them in front of me. I then consume what he has given me and go on my merry way.

But what does it take to use these services that Chris has laid bare before us? Well, that's what this article is about, read on.

Creating a Win Forms application

For this article I will use a C# Windows application as my Web Service consumer, but you can consume Web Services from an ASP.NET web page or any other .NET project using VB.NET, Managed C++, or any other .NET language if you like. The demo project covered here, will retrieve the latest articles from the CodeProject.

Creating the application is beyond the scope of this article so I will assume you already know how to do it. If not, here are some references:

Adding the Web reference

To use/consume a Web Service, you must first tell your application where to find it.

In the Solution Explorer, right click on your project and click "Add Web Reference..." or from the Project menu, click "Add Web Reference...". In the address line, type in the URL to the Web Service. For this example, use

You should see a LatestBrief web page in the browser portion of the window.

Click the Add Reference button to finish adding the Web Reference to the project.

Using the Web reference

When the Web reference is added, VS.NET uses the WSDL file to create a Reference.cs containing the C# namespace and classes defined in the WSDL (the CP Cafe menu).

You will use the namespace, classes and methods in your application to consume the CodeProject's "beer and latest articles".

Consumin' CP's Web services

Since the Web Reference is added, we can move onto some code. Unfortunately, you can't really order a beer from CP (yet ;) ), but let's go ahead and see how we can get the latest articles from the CP Web service.


namespace CPConsumer
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class CPConsumerForm : System.Windows.Forms.Form

        private LatestBrief cpLatestBrief = null;

        public CPConsumerForm()
            // Required for Windows Form Designer support

            // instantiate the LatestBrief class
            cpLatestBrief = new LatestBrief();

Add using;, the Web Reference namespace, for convenience.

Declare a private member variable of the Form called cpLatestBrief of the type LatestBrief and set it to null. This is the class generated via the WSDL that we will use. In the constructor we instantiate it using new.

private int GetNumArticles()
    // get the maximum number of article briefs we can get from CP
    if (cpLatestBrief != null)
        return cpLatestBrief.GetMaxArticleListLength();
        return 0;

Next, we want to find the maximum number of articles we can get from the web service. We do this by using the GetMaxArticleListLength function defined in the WSDL and the LatestBrief class.

We just consumed part of the CP Web service!

// article brief array
ArticleBrief [] cpArticles = null;

// max article briefs
int iNumArticles = GetNumArticles();

// clear the list

if (iNumArticles > 0 && cpLatestBrief != null)
    // get the article briefs from the web service
    cpArticles = cpLatestBrief.GetLatestArticleBrief(iNumArticles);

    if (cpArticles != null)
        // add them all to the list view
        for (int i=0; i<iNumArticles; i++)
            ListViewItem lvi = new 




In the demo project, I fill a list view with the Latest CP articles. To get these, create an ArticleBrief array to be filled by the GetLatestArticleBrief method.

After the array is filled, the ListView is populated with the article details. Selecting an article in the ListView will provide you with a link and the description in labels, below the ListView.


I would have never guessed that Web services would be so easy to use. Thank you Chris M. for making the CodeProject Web services available to us.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jason Henderson
United States United States
I have been a professional developer since 1996. I live in the middle of no where in Illinois, USA. I am married and have four children.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
Ivanzillo23-Dec-10 6:59
memberIvanzillo23-Dec-10 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 15 Aug 2002
Article Copyright 2002 by Jason Henderson
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid