Issue dated - 9th September 2003

-


Previous Issues

CURRENT ISSUE
INDIA NEWS
STOCK FILE
FOCUS
INDIA TRENDS
NEWS ANALYSIS
OPINION
COMPANY WATCH
TECHSPACE
TECHNOLOGY
E-BUSINESS
BOOK REVIEWS
EVENTS
PRODUCTS
COLUMNS
TECH FORUM

THE C# COLUMN

BETWEEN THE BYTES
TECHNOLOGY
SPECIALS <NEW>
Symantec Report
Security Headquarters
JobsDB
MINDPRINTS
HMA BANKBIZ
EC SERVICES
ARCHIVES/SEARCH
IT APPOINTMENTS
WRITE TO US
SUBSCRIBE/RENEW
CUSTOMER SERVICE
ADVERTISE
ABOUT US

 Network Sites
  IT People
  Network Magazine
  Business Traveller
  Exp. Hotelier & Caterer
  Exp. Travel & Tourism
  Exp. Pharma Pulse
  Exp. Healthcare Mgmt.
  Express Textile
 Group Sites
  ExpressIndia
  Indian Express
  Financial Express

 
Front Page > TechSpace > Story Print this Page|  Email this page

Creating an ASP.NET client for WebService

The C# Column - Yashawant Kanetkar

In the last article we had created a temperature Web service with two methods ctof() and ftoc(). The ftoc() method converts the temperature in Fahrenheit to Celsius and creates a WinForm-based client for it. Now let us take a look at how to create an ASP.NET Web Form client for our Web Service.

Create an ASP.NET Web Application called ‘temperatureclient’. Design the Web form as shown in the following figure using the controls from the Toolbox.

We have changed the background color of the table, font and font size in the ‘Build Style’ option. Select this option from the pop-up menu that appears after right-clicking on the form. We have also changed the background colors of the label and button control by changing their respective properties. Change the names of controls as shown in the following table.

Control Name
Text box ttemp
Celsius button cb
Fahrenheit buton fb
Label ltemp

The form stands created. Now we will add the reference of the Web Service assembly in our project. For this, right-click ‘Solution Explorer’ and select the ‘Add Web Reference’ option from the pop-up menu. On selecting this option, the ‘Add Web Reference’ window gets displayed. Enter the address as ‘http:\ \ localhost\ webservice\ Service1.asmx’ (Name of the temperature Web Service project is ‘webservice’). Click the ‘Add Reference’ button on the dialog box.

On adding the reference, a folder ‘Web References\ localhost’ gets created in the project directory containing the four files namely, ‘Reference.cs’, ‘Reference.map’, ‘Service1.wsdl’ and ‘Service1.disco’.

Rename the namespace ‘localhost’ to ‘currencyservice’. For this, right-click ‘Web References\ Localhost’ in Solution Explorer and select ‘Rename’ from the pop-up menu. Also add the reference to the currencyservice namespace at the beginning of the program:

using temperatureclient.currencyservice ;

Add the Click event handlers for both the buttons. These button handlers are given below:

private void cb_Click ( object sender, EventArgs e )
{
temperature t = new temperature( ) ;
double f = Double.Parse ( ttemp.Text ) ;
double d = t.ftoc ( f ) ;
ltemp.Text = d.ToString( ) ;
}

private void fb_Click ( object sender, EventArgs e )
{
temperature t = new temperature( ) ;
double c = Double.Parse ( ttemp.Text ) ;
double d = t.ctof ( c ) ;
ltemp.Text = d.ToString( ) ;
}

In these handlers we have created a reference to the temperature class’s object and called the methods using it.

Run the program. Our Web form gets displayed. The following figure displays the screenshot taken after the Web Service method ftoc() is invoked.

Synchronous & Asynchronous communication

Web Services support both synchronous and asynchronous communication between the client and the server that hosts the Web Service. Under synchronous communication, the client sends a request to the server and waits for the response. This prevents the client from performing other operations while waiting for the results. On the other hand, in asynchronous communication, the client continues processing other tasks as it waits for a response. The client responds to the result of the service request when it becomes available.

When a proxy class is created by .NET using the wsdl tool, it generates the synchronous and asynchronous versions of the methods in the class. The asynchronous versions consist of two methods having names as ‘Beginmethodname’ and ‘Endmethodname’. The Begin method is used to initiate the Web Service, while the End method is used to retrieve the results.

The handler cb_Click() given below shows how to make asynchronous call to the ftoc() method.

private void cb_Click ( object sender, EventArgs e )
{
AsyncCallback cb = new AsyncCallback ( ftoccallback ) ;
float f = float.Parse ( ttemp.Text ) ;
t.Beginftoc ( f, cb, t ) ;
// Do some work
}

Here, we have firstly created a reference to the AsyncCallback delegate class. The delegate object cb represents the method ftoccallback(). Next, we have retrieved the value entered in the text box and called the Beginftoc( ) method. To the Beginftoc() method, we have passed the temperature in Farehnheit, reference to the callback object, and reference to the temperature class’s object. Add ftoccallback() as method of the WebForm1 class. This method is given below.

public void ftoccallback ( IAsyncResult ar )
{
temperature t = ( temperature ) ar.AsyncState ;
double d = t.Endftoc ( ar ) ;
ltemp.Text = d.ToString( ) ;
}

Here, we have obtained the reference to the temperature class’s object. Using this reference, we have called the Endftoc() method to collect the return value, which is then displayed in the label.

Yashavant Kanetkar, one of the first Express Computer columnists, is an established software expert, speaker and author with several best-sellers to his credit, including titles like “Let Us C” and the “Fundas” series. Contact him at kanet@nagpur.dot.net.in
<Back to top>


© Copyright 2003: Indian Express Group (Mumbai, India). All rights reserved throughout the world. This entire site is compiled in
Mumbai by The Business Publications Division of the Indian Express Group of Newspapers.
Please contact our Webmaster for any queries on this site.