v. con·sumed, con·sum·ing, con·sumes
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.
public class CPConsumerForm : System.Windows.Forms.Form
private LatestBrief cpLatestBrief = null;
cpLatestBrief = new LatestBrief();
using CPConsumer.com.codeproject.www;, the Web Reference namespace, for convenience.
Declare a private member variable of the
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
private int GetNumArticles()
if (cpLatestBrief != null)
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
We just consumed part of the CP Web service!
ArticleBrief  cpArticles = null;
int iNumArticles = GetNumArticles();
if (iNumArticles > 0 && cpLatestBrief != null)
cpArticles = cpLatestBrief.GetLatestArticleBrief(iNumArticles);
if (cpArticles != null)
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
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
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.