|
Creating & consuming Web services
The C# Column - Yashawant Kanetkar
Last
time we had created a Web service project and executed it without adding our
Web methods to it. In this article we will first complete the creation of the
Web service.
When we create a Web service project named
‘webservice’, a namespace having the name webservice and a class having the
name Service1 gets created. Change the class name to temperature. Add the public
methods ctof( ) and ftoc( ) to the class temperature. These methods are given
below.
[ WebMethod ]
public double ctof ( double c )
{
double f = ( c * 9.0 / 5.0 ) + 32 ;
return f ;
}
[ WebMethod ]
public double ftoc ( double f )
{
double c = 5.0 / 9.0 * ( f - 32 ) ;
return c ;
}
A Web service class can contain methods
that can be called by the client or by the methods that are used internally
within the class. To expose a method as a Web service so that it can be called
by the clients, we have to add an attribute [ WebMethod ] to it. Note that the
methods to be exposed as Web services must be declared public. Now compile and
execute the program by pressing Ctrl + F5. You will see a window as shown in
the following figure:

Note that the ‘Address’ bar contains the
URL ‘http://localhost/webservice/Service1.asmx’. The ‘.asmx’ file acts as an
entry point to the Web service. The statement written in the ‘Service1.asmx’
file is given below:
<%@ WebService Language="c#"
Codebehind="Service1.asmx.cs"
Class="webservice.temperature" %>
Here, WebService is an ASP.NET tag, which
states that the code written in the specified class is to be exposed as a Web
service. The Codebehind tag specifies the name of the source file in which the
Web service is defined. We can omit this tag and write the Web service code
in the ‘.asmx’ file itself. In fact, defining the Web service in the ‘.asmx’
file itself facilitates the deployment of the service.
The ‘Service1.asmx’ file contains fully
qualified name of class (webservice.temperature) in which our methods ctof(
) and ftoc( ) are defined. When the ‘.asmx’ file is executed all those methods
that are exposed as Web services using the [ WebMethod ] attribute get displayed
in the output window.
If we click the ctof link a page, as shown
in the following figure, gets displayed.

Enter a number representing the temperature
in centigrade and click the ‘Invoke’ button. The ctof( ) method gets called
and the output is generated in XML. You will appreciate how testing a Web service
is being made easy. When we build the project, the ‘bin’ folder gets created
in the ‘Debug’ folder and ‘webservice.dll’ gets created in it.
We will now create a Winform application
that uses this Web service. Create the application and design the form as shown
in the following figure:

Name the text box as ttemp, label as ltemp
and buttons as cb and fb respectively.

We must add a reference to the ‘webservice.dll’
we are going to use in this application. For this, right-click on the project
name in ‘Solution Explorer’ and select ‘Add Web References’ from the pop-up
menu. The window shown above will appear.
Enter the address as ‘http:\ \ localhost\
webservice\ Service1.asmx’. To use a Web service, which is available on a different
machine, say User1, add its Web reference by specifying the URL as ‘http://User1/
webservice/Service1.asmx’. Click the arrow present on the right side of the
‘Address’ text box. A dialog box displaying a page with links to the Web methods
gets displayed. Click the ‘Add Reference’ button on the
dialog box.
On doing this, a folder ‘WebReferences\
localhost’ gets created in the project directory. In the ‘Localhost’ folder
four files namely, ‘Reference.cs’, ‘Reference.map’, ‘Service1.wsdl‘ and ‘Service1.disco’
get generated.
The ‘Reference.cs’ file contains the proxy
class discussed in the last article. The ‘Reference.map’ file contains the reference
in the form of URLs of the ‘.asmx’ and the discovery files. The files ‘Service1.wsdl’
and ‘Service1.vsdisco’ are the WSDL and discovery files respectively.
By default the proxy class gets defined
in the WSclient.localhost namespace. Instead of the localhost name we can give
some meaningful name to the namespace. We can rename the localhost namespace
from ‘Solution Explorer’. For this, right-click the localhost name and select
‘Rename’ from the pop-up menu. We will define the namespace as tempservice.
Let us now add Click event handlers for
both the buttons on the form. These handlers are given below.
private void cb_Click ( object sender, System.EventArgs
e )
{
temperature t = new temperature( ) ;
double f = Double.Parse ( ttemp.Text ) ;
double d = t.ftoc ( f ) ;
ltemp.Text = d.ToString( ) ;
}
private void bf_Click ( object sender, System.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 used the methods
exposed by our Web service. We have simply created a reference to the temperature
class’s object and called the methods using it. Thus, using a Web service is
as simple as using a class.
As said earlier, the proxy class is defined
in the tempservice namespace, so we must add its declaration at the beginning
of the program as:
using WSclient.tempservice ;
Run the program. Enter temperature in Celsius
in the text box and click the ‘Get Fahrenheit’ button. You will see the temperature
in Fahrenheit displayed on 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 |
|