Click here to Skip to main content
Click here to Skip to main content

Consuming Web Services from a Win Forms Application

By , 14 Aug 2002
 

Sample Image - cpwebserviceconsumer.jpg

Introduction

con·sume

v. con·sumed, con·sum·ing, con·sumes
v. tr.
...
   2.
      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 http://www.codeproject.com/webservices/latest.asmx.

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.

...
using CPConsumer.com.codeproject.www;

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
            //
            InitializeComponent();

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

Add using CPConsumer.com.codeproject.www;, 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();
    else
        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
ArticleList.Items.Clear();

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 
                  ListViewItem(cpArticles[i].Updated.ToString());

            lvi.SubItems.Add(cpArticles[i].Title);
            lvi.SubItems.Add(cpArticles[i].Author);
            lvi.SubItems.Add(cpArticles[i].Description);
            lvi.SubItems.Add(cpArticles[i].URL);

            ArticleList.Items.Add(lvi);
        }

        ...
    }
}

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.

Conclusion

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.

License

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
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMember 781804215 Jan '13 - 3:13 
GeneralMy vote of 4memberIvanzillo23 Dec '10 - 5:59 
GeneralMy vote of 1memberMember 436084221 May '10 - 2:01 
Generalgood articlememberDonsw19 Jan '09 - 10:26 
GeneralVery helpful.membertmith12 Jan '09 - 9:12 
QuestionHow to run this project in linux by using monomemberSaumya1230 Aug '07 - 19:26 
GeneralWeb Service GONE!memberBrad Bruce2 Mar '07 - 4:33 
GeneralTo List All web Methods in a given .asmx file by taking the URL as inputmembercompdesign14 Jun '06 - 23:20 
GeneralRe: To List All web Methods in a given .asmx file by taking the URL as inputmemberMadhu KG28 Jan '09 - 2:52 
GeneralRe: To List All web Methods in a given .asmx file by taking the URL as inputmemberJason Henderson28 Jan '09 - 3:25 
GeneralProblem Consuming a Web ServicememberSebina10 May '06 - 6:25 
GeneralWeb Services in Visual Studio 2005memberPadmavathy Thiruvenkadam6 Oct '05 - 20:48 
QuestionHow do I terminate the Session from the Windows Appmembervmo3d8 Aug '05 - 9:40 
AnswerRe: How do I terminate the Session from the Windows AppmemberJason Henderson8 Aug '05 - 11:03 
GeneralhellomemberAhmad Abu shareha8 Aug '05 - 6:21 
GeneralThanks !memberTrance Junkie5 Jul '05 - 22:09 
QuestionWhere I can Find the LatestBrief Sample ?sussAnonymous15 Feb '05 - 22:50 
AnswerRe: Where I can Find the LatestBrief Sample ?memberJason Henderson16 Feb '05 - 3:47 
GeneralArticle looks good but needs updatesmemberJeffrey Scott Flesher13 Sep '04 - 19:35 
GeneralMobile devicesmembermschuenck1 Oct '03 - 15:35 
GeneralError: Proxy Authentication Requiredmemberpkhc26 Sep '03 - 0:51 
GeneralRe: Error: Proxy Authentication Requiredmembercedric5723 Apr '04 - 4:10 
GeneralDynamically...memberArkadiusz Merta25 Mar '03 - 3:04 
GeneralRe: Dynamically...memberJason Henderson25 Mar '03 - 4:32 
GeneralRe: Dynamically...memberBrian Lyttle18 Apr '03 - 0:09 

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

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