Click here to Skip to main content
14,978,385 members
Articles / Desktop Programming / MFC
Posted 14 Aug 2002


107 bookmarked

Consuming Web Services from a Win Forms Application

Rate me:
Please Sign up or sign in to vote.
3.78/5 (40 votes)
14 Aug 20023 min read
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.

Image 2

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
Software Developer (Senior)
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.

Comments and Discussions

QuestionWhat Files are needed when deploying a WinForms app with a Service Reference? Pin
CRT Dev Team 131-Jan-18 10:55
MemberCRT Dev Team 131-Jan-18 10:55 
GeneralMy vote of 5 Pin
Member 781804215-Jan-13 3:13
MemberMember 781804215-Jan-13 3:13 
GeneralMy vote of 4 Pin
Ivanzillo23-Dec-10 5:59
MemberIvanzillo23-Dec-10 5:59 
GeneralMy vote of 1 Pin
Member 436084221-May-10 2:01
MemberMember 436084221-May-10 2:01 
Generalgood article Pin
Donsw19-Jan-09 10:26
MemberDonsw19-Jan-09 10:26 
GeneralVery helpful. Pin
tmith12-Jan-09 9:12
Membertmith12-Jan-09 9:12 
QuestionHow to run this project in linux by using mono Pin
Saumya1230-Aug-07 19:26
MemberSaumya1230-Aug-07 19:26 
GeneralWeb Service GONE! Pin
Brad Bruce2-Mar-07 4:33
MemberBrad Bruce2-Mar-07 4:33 
GeneralTo List All web Methods in a given .asmx file by taking the URL as input Pin
compdesign14-Jun-06 23:20
Membercompdesign14-Jun-06 23:20 
GeneralRe: To List All web Methods in a given .asmx file by taking the URL as input Pin
Madhu K G28-Jan-09 2:52
MemberMadhu K G28-Jan-09 2:52 
GeneralRe: To List All web Methods in a given .asmx file by taking the URL as input Pin
Jason Henderson28-Jan-09 3:25
MemberJason Henderson28-Jan-09 3:25 
GeneralProblem Consuming a Web Service Pin
Sebina10-May-06 6:25
MemberSebina10-May-06 6:25 
GeneralWeb Services in Visual Studio 2005 Pin
Padmavathy Thiruvenkadam6-Oct-05 20:48
MemberPadmavathy Thiruvenkadam6-Oct-05 20:48 
QuestionHow do I terminate the Session from the Windows App Pin
vmo3d8-Aug-05 9:40
Membervmo3d8-Aug-05 9:40 
AnswerRe: How do I terminate the Session from the Windows App Pin
Jason Henderson8-Aug-05 11:03
MemberJason Henderson8-Aug-05 11:03 
Generalhello Pin
Ahmad Abu shareha8-Aug-05 6:21
MemberAhmad Abu shareha8-Aug-05 6:21 
GeneralThanks ! Pin
Trance Junkie5-Jul-05 22:09
MemberTrance Junkie5-Jul-05 22:09 
QuestionWhere I can Find the LatestBrief Sample ? Pin
Anonymous15-Feb-05 22:50
MemberAnonymous15-Feb-05 22:50 
AnswerRe: Where I can Find the LatestBrief Sample ? Pin
Jason Henderson16-Feb-05 3:47
MemberJason Henderson16-Feb-05 3:47 
GeneralArticle looks good but needs updates Pin
Jeffrey Scott Flesher13-Sep-04 19:35
MemberJeffrey Scott Flesher13-Sep-04 19:35 
GeneralMobile devices Pin
mschuenck1-Oct-03 15:35
Membermschuenck1-Oct-03 15:35 
GeneralError: Proxy Authentication Required Pin
pkhc26-Sep-03 0:51
Memberpkhc26-Sep-03 0:51 
GeneralRe: Error: Proxy Authentication Required Pin
cedric5723-Apr-04 4:10
Membercedric5723-Apr-04 4:10 
GeneralDynamically... Pin
Arkadiusz Merta25-Mar-03 3:04
MemberArkadiusz Merta25-Mar-03 3:04 
GeneralRe: Dynamically... Pin
Jason Henderson25-Mar-03 4:32
MemberJason Henderson25-Mar-03 4:32 

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.