This post contains attachments v20020421194205.zip 
Summary
This article explains how to create Temperature Conversion XML WebService and how to consume the WebService by creating client Web Form and client Console application.
This article explains
how we can create the XML WebService for Temperature Conversion
how to create a proxy class
how to consume the XML WebService by creating a Client Web Form page
how to consume the XML WebService by creating a Client Console application
Creation of WebService
The XML WebService file should have .asmx extension.
In this file, we need to declare webservice directive as follows:
<%@ WebService Language=VB Class=TemperatureConversion>
After declaring the WebService as above, create the WebService Class as follows: VB.NET Code
<WebService(Namespace:=http://pandu/webservices)> _
Public Class TemperatureConversion
Inherits WebService
<WebMethod(Description:=Convert Fahrenheit Degrees to Celsius Degrees)> _
Public Function FahrenheitToCelsius(nFValue As Double) As Double
Return 5/9 * (nFValue -32)
End Function
<WebMethod(Description:=Convert Celsius Degrees to Fahrenheit Degrees)> _
Public Function CelsiusToFahrenheit(nCValue As Double) As Double
Return (9/5 * nCValue) + 32
End Function
End Class
Here the class TemperatureConversion inherits WebService class of System.Web.Servies Namespace because it can access Application, Session and other ASP.NET objects that give information about the HTTP request that invokes the WebService.
<WebService()> attribute is used to specify XML namespace. XML Web Services use XML namespaces to uniquely identify the listener where a client sends requests.
<WebMethod()> attribute is used to expose Web Service methods as public
Create a Proxy class
After creating the WebService, we need to get the WSDL contract of WebService as follows:
wsdl /l:vb /o:TempConversion.vb http://masabathula/tc/Tempconversion.asmx?wsdl /n:PanduTest
Here wsdl.exe is the .NET utility, which can be executed at the command prompt.
/l indicates the language, /o indicates the output file, /n indicates the namespace and http://masabathula/tc/Tempconversion.asmx?wsdl is the WSDL of the asmx file.
This above instruction at command prompt creates a TempConversion.vb file.
Now compile this file as follows to get the Proxy class:
vbc /out:TempConversion.dll /t:library /r:system.web.dll,system.dll,system.xml.dll,system.web.services.dll,system.data.dll TempConve
rsion.vb
Here vbc.exe is .NET utility, which can be executed at the command prompt.
The above command creates a TempConversion.dll file, which is called the proxy class or the assembly class.
Consuming the WebService by creating a Client Web Form Page
To consume the WebService, TempConversion.dll should be available from bin folder of the application domain.
If there is no bin folder, create a bin folder and copy the TempConversion.dll into that folder.
Now create and write the ClientTempConv.aspx file as follows:
<%@ page language=c# debug=true %>
<%@Import Namespace=PanduTest %>
<%@Import Namespace=System %>
<script language=c# runat=server>
TemperatureConversion oTemp = new TemperatureConversion();
void ConvertToFahrenheit(System.Object Sender, System.EventArgs e)
{
try
{
Double nCelsius = Double.Parse(txtCelsius.Text);
lblResult.Text = txtCelsius.Text + Degrees Celsius = + oTemp.CelsiusToFahrenheit(nCelsius).ToString() + Degrees Foreinheit.;
}
catch
{
lblResult.Text = Invalid input. Please try again!;
}
}
void ConvertToCelsius(System.Object Sender, System.EventArgs e)
{
try
{
Double nFahrenheit = Double.Parse(txtFahrenheit.Text);
lblResult.Text = txtFahrenheit.Text + Degrees nFahrenheit = + oTemp.FahrenheitToCelsius(nFahrenheit).ToString() + Degrees Celsius.;
}
catch
{
lblResult.Text = Invalid input. Please try again!;
}
}
</script>
<html>
<head>
<title> </title>
</head>
<body>
<form id=frmConvert method=post runat=server>
<table>
<tr>
<td>
<asp:TextBox id=txtFahrenheit text= runat=server></asp:TextBox> Degrees Fahrenheit =
</td>
<td>
<asp:Button id=btnFahrenheit text=Convert to Celsius runat=server onClick=ConvertToCelsius ></asp:Button>
</td>
</tr>
<tr>
<td>
<asp:TextBox id=txtCelsius text= runat=server></asp:TextBox> Degrees Celsius =
</td>
<td>
<asp:Button id=btnCelsius text=Convert to Fahrenheit runat=server onClick=ConvertToFahrenheit ></asp:Button>
</td>
</tr>
</table>
<br />
<% if (IsPostBack) { %>
<asp:Label id=lblResult width=100% height=25px runat=server></asp:Label>
<% } %>
</form>
</body>
</html>
Here we are required to import the Namespace PanduTest to access TemperatureConversion class.
In the above example, there are two text boxes one for Celsius Degrees and other for Fahrenheit Degrees.
There are two button controls – one to convert Celsius Degrees to Fahrenheit Degrees and the second to convert Fahrenheit Degrees to Celsius Degrees. There is one label control to display the message on user actions.
Consuming the WebService by creating a Client Console application
Create the console based ClientTempConv.vb client as follows:
- Import the namespaces as follows:
Imports PanduTest
Imports System
Imports System.Data
Imports System.Web.Services
Imports System.Xml
Imports Microsoft.VisualBasic
- Create a class Temperature as follows:
Public Class Temperature
Protected nFahrenheit As Double
Protected nCelsius As Double
Dim oTemp As TemperatureConversion
Public Sub New()
oTemp = new TemperatureConversion()
End Sub
Public Function GetFahrenheit(nC As Double)
return(oTemp.CelsiusToFahrenheit(nC))
End Function
Public Function GetCelsius(nF As Double)
return(oTemp.FahrenheitToCelsius(nF))
End Function
End Class
Here oTemp is instance of TempConversion of WebService.
- Create a class TestTemperature as follows:
Class TestTemperature
Public Shared Sub Main()
Dim oTemp As Temperature
oTemp = New Temperature()
Console.WriteLine(Using VB.NET ...)
Console.WriteLine(10 Degrees Celsius = & oTemp.GetFahrenheit(10) & Degrees Fehreinheit)
Console.WriteLine(20 Degrees Celsius = & oTemp.GetFahrenheit(20) & Degrees Fehreinheit)
Console.WriteLine(10 Degrees Fahrenheit = & oTemp.GetCelsius(10) & Degrees Celsius)
Console.WriteLine(20 Degrees Fahrenheit = & oTemp.GetCelsius(20) & Degrees Celsius)
End Sub
End Class
Here oTemp is the instance of Temperature class.
- Compile the ClientTempConv.vb file as follows:
vbc /r:System.dll,System.Data.dll,Microsoft.VisualBasic.dll,System.Xml.dll,System.Web.Services.dll,TempConversion.dll ClientTempConv
.vb
This creates an executable file ClientTempConv.exe.
System Requirements: Microsoft.NET Framework
|