Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

A PriceCheck Client in .NET

Let's take a look at building a simple Web service client in the .NET framework. Note that we're using the .NET framework beta 2, which is the current version as of August 2001. By the time this book is in your hands, a later .NET release will probably be available.

A lot of this process is even easier using Visual Studio .NET, but for the client examples, we'll use the command-line tools; these are all you'll have available if you're a Windows 98 user, and we want to maximize the potential that you'll be able to run these examples on your machine.

Because the .NET Web service tools are all deeply integrated with WSDL, we'll start by building a client for the SkatesTown price check service.

To begin, we want to build a proxy class to access the service based on the WSDL file, so to do so we'll use the wsdl.exe tool that is provided in the Bin/ directory of the .NET framework SDK installation. (You should have this directory on your path as a result of the .NET SDK setup.) The command and its results are as follows:

C:\MyServices> wsdl PriceCheck.wsdl

Microsoft (R) Web services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.0.2914.16]
Copyright (C) Microsoft Corp. 1998-2001. All rights reserved.

Writing file 'C:\MyServices\PriceCheckService.cs'.

Now you'll notice a PriceCheckService.cs file in the current directory. This is the C# source code that was generated by the tool—take a look at it if you're interested; it's pretty straightforward. You'll notice that this class has exactly the same methods we exposed in the Java back-end class—it is a proxy for our Web service and can now be used by any .NET application to get at our price check functionality. The file also contains a C# version of our AvailabilityType class.

We could have also chosen to generate the code in other languages, such as Visual Basic, but we'll stick with C# for our purposes because it's very comprehensible to Java developers.

To invoke the service, we'll need to wrap the service proxy class in a small bit of user interface so that we can use it. We open our favorite text editor and build the following C# code:

using System;

class PriceCheckClient {
  public static void Main(string [] args)
  {
    PriceCheckService stub = new PriceCheckService();
    availabilityType avail = stub.checkPrice("947-TI");
    Console.WriteLine("There are " + avail.quantityAvailable +
[ccc] " items available, ");
    Console.WriteLine("at a price of $" + avail.price.ToString() +
[ccc] " each. ");
  }
}

Clearly, this is an extremely basic little demonstration, with a hard-coded SKU argument. The point is simply to show you how similar using stubs in C#/.NET is to the same activity in Java/Axis.

We compile the application like so:

C:\MyServices> csc /r:System.Web.Services.dll 
[ccc] /r:System.Xml.dll /r:System.dll 
[ccc] PriceCheckClient.cs PriceCheckService.cs
Microsoft (R) Visual C# Compiler Version 7.00.9254 
[ccc] [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

csc is the C# compiler/linker. The /r flag tells the compiler to reference the indicated assemblies (an assembly in .NET is a packaged set of components, in a DLL or an EXE) and make their contents available during the build process. This command should result in creating a PriceCheckClient.exe in your current directory. If you run that program, you'll be able to make priceCheck requests from our SkatesTown database (see Figure 1).

Figure 1 PriceCheck with a .NET client.

  • + Share This
  • 🔖 Save To Your Account