Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Consuming Web Services from a Win Forms Application

0.00/5 (No votes)
14 Aug 2002 1  
How to access and use a web service from a C# application, using the CodeProject Web Services.

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