Delphi.net Basics
  Home  |  Delphi .net Home  |  .Net Web Services
 .NET Framework
 Namespace References

 System
 System.Collections
 System.Drawing - download only
 System.Globalization
 System.IO

 Articles and Tutorials

 Overview of .NET
 Delphi and .NET
 Winform Applications
 ASP .Net Applications
 ASP Web Services
 Framework Collections
 Framework String Handling
 Framework Files and Folders


 Author links

 

 
 
 .Net Web Services
What is a Web Service?
Before I answer that question, let's step back a bit.
 
The Internet is known by most people as a place to search for web pages. The pages may be static html, or dynamically built as you request them. But the Internet can do a lot more than return pages. Because it is a communication medium, programs as well as users can communicate. Before Web Services, a mechanisms called Remote Procedure Call (RPC) allowed a program to call another program on another server.
 
Web Services take this concept and both formalise and extend it. A Web Service is essentially an application that can be invoked remotely by another program or a user. It receives and returns data using XML (eXtensible Markup Language) via HTTP or HTTPS (Hyper Text Transfer Protocal/Secure) as the communications medium. The XML formatting style is governed by SOAP (Simple Object Access Protocal), since objects can be passed in both directions.
 

What would you use a Web Service for?
This is a very good question. At the time of writing, Web Services are in an infant stage of development, and are likely to evolve significantly over the years.
 
Web services currently provide dynamic data, such as stock market movement, the latest football scores and weather in different locations, processing services, such as accountancy functions, booking services, such as hotels and holidays and so on.
 
They are designed to do things that bring a bit of the outside World into your application, or provide pre-built functionality that you can use to extend the scope of your application.
 
The idea is to be able to do things like finding and booking a hotel via your PDA when you arrive in city on holiday. Or getting your PC to flash a message when your stocks hit a certain value. A dynamic use of the Internet.
 

Using a Web Service
I'll discuss here a trivial web service that I found at www.salcentral.com which provides temperature, weight and other conversions. SalCentral is a search engine for Web Services. I found www.sharptooth.ws when looking for conversion.
 
If you go to the UCWS/UniversalConversionService page, you will see the Web Services provided by sharptooth :
 
 ConvertMassOrWeight
   Returns a converted mass / weight value
 
 ConvertTemperature
   Returns a converted temperature value
 
 ConvertLength
   Returns a converted length value
 
 Message
   Service operation specific information guide

Click on the ConvertTemperature link and you will be asked for a temparature to convert - yes, you can run the Web Service directly via this web page! Web Services can often (but not always) be used from a web browser as well as an application.
 
The ConvertTemperature service asks for two values :
 
 conversionType String
 tempToConvert  String

You need to tell it the from and to temperature scales to use in the conversion. Unfortunately, it is not obvious hoe to specify this. If you click on the Message service, all will be revealed as the following information is shown :
 
 Each service operation takes in two 'string' parameters, corresponding to the conversion type and the value to be converted respectively. Conversion types are represented by numbers (ints) and the value to be converted should be in a decimal format (e.g. 3.147).
 For ConvertLength the conversion types are as follows:
 1 - Millimetres to Inches,
 2 - Centimetres to Inches,
 3 - Metres to Feet,
 4 - Metres to Yards,
 5 - Kilometres to Miles,
 6 - Inches to Millimetres,
 7 - Inches to Centimetres,
 8 - Feet to Metres,
 9 - Yards to Metres,
 10 - Miles to Kilometres.
 For ConvertMassOrWeight the conversion types are as follows:
 1 - Milligrams (mg) to Grains (gr),
 2 - Grams (g) to Drams (dr),
 3 - Kilograms (kg) to Pounds (lb),
 4 - Kilograms (kg) to Stones,
 5 - Grains (gr) to Milligrams (mg),
 6 - Drams (dr) to Grams (g),
 7 - Pounds (lb) to Kilograms (kg),
 8 - Stones to Kilograms (kg).
 For ConvertTemperature the conversion types are as follows:
 1 - Kelvin to Celsius,
 2 - Kelvin to Fahrenheit,
 3 - Celsius to Kelvin,
 4 - Celsius to Fahrenheit,
 5 - Fahrenheit to Celsius,
 6 - Fahrenheit to Kelvin.

So, to convert from Farenheit to Celsius, you must type in '5' as the conversionType string. Try it and see what happens!
 

Using a Web Service in your application
You can use this service in your application. We'll build a very simple application that provides a Farenheit to Celsius conversion function. This is merely illustrative - Delphi has its own such conversion functions!
 
First, open a new Windows Forms Application, and save it now.
 
Then we will add our web service straight away by right clicking the Project1.exe name in the Files list in the Project Manager window. Select Add Web reference from the pop menu that appears :
 

 
In the top of the form that appears, enter the following url :
 
http://www.sharptooth.ws/UCWS/UniversalConversionService.asmx?WSDL
 
The WSDL suffix requests that Web Service Description Language be used to reveal the nature of the services being offered.
 
Press enter or the little blue arrow to see the web service as XML text. You should then also see ws.sharptooth.www as the reference for the url that you can add to your application. Note the reverse hierarchy of naming - the www comes last! Click on the Add Reference button, and a while later it should add the web reference to your project.
 
If you open out the added reference, you will see the following :
 

 
The sharktooth.UniversalConversionService.pas unit was automatically built by Delphi from the WSDL page - it is a self-contained wrapper to the web service, and we should now add it to the Uses clause of our program :
 
 uses
   System.Drawing, System.Collections, System.ComponentModel,
   System.Windows.Forms, System.Data,
   sharptooth.UniversalConversionService;

To use this service, add label, textbox and button components to the form so that it looks like this :
 

 
with the following component names :
 
 Label1            for the 'Fahrenheit :' label
 FahrenheitTextBox for the number entry box
 ConvertButton     for the button
 Label2            for the 'Celsius :' label
 CelsiusLabel      for the currently blank result value label

We must define a variable to hold our webr service object wrapper. Do this in a var block just after the implementation :
 
 implementation
 
 var
   // Our web service as an object reference
   Converter : sharptooth.UniversalConversionService.UniversalConversion;

Double click the form to generate a constructor for the form, where we can create the object wrapper for our web service :
 
 constructor TWinForm.Create;
 begin
   inherited Create;
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent;
 
   // Create an object that represents our web service
   Converter :=
            sharptooth.UniversalConversionService.UniversalConversion.Create;
 end;

Double click the button to generate action code for the button, and type in the following to use the web service :
 
 procedure TWinForm.ConvertButton_Click(sender: System.Object;
                                        e: System.EventArgs);
 begin
   // Convert the Fahrenheit value to Celsius and display
   // The conversion type of '5' means Fahrenheit to Celsius
   CelsiusLabel.Text := FloatToStr(Converter.ConvertTemperature('5',
                                   FahrenheitTextBox.Text));
 end;

Now we can run the code. After typing in a value, say 70, and pressing the button, a delay will occur before the result is displayed :
 

 
Note that there are 2 other methods associated with temperature conversion :
 
 BeginConvertTemperature
 EndConvertTemperature

The first of these is an asynchronous version of the ConvertTemperature method, allowing your application to do other things while you wait for the response fronm the web server. EndConvertTemperature allows you to force a wait if need be. Most web servers provide these asynchronous flavours of their services.
 

Writing a Web Service
This is beyond the current scope of Delphi Basics. To develop Web Services, you will need to be running IIS or a similar server - the Professional variety of Windows XP is needed to do this - in order to test the services. You would then deploy to an appropriate Internet server, and make register your service with a web service serach engine.
 
 
'
'
'
Delphi Programming © Neil Moffatt All rights reserved.  |  Home Page