Web Service Example
Any application can have a Web Service component.
Web Services can be created regardless of programming language.
An example ASP.NET Web Service
In this example we use ASP.NET to create a simple Web Service.
<%@ WebService Language="VBScript" Class="TempConvert" %>
Imports System
Imports System.Web.Services
Public Class TempConvert :Inherits WebService
<WebMethod()> Public Function FahrenheitToCelsius
(ByVal Fahrenheit As String) As String
dim fahr
fahr=trim(replace(Fahrenheit,",","."))
if fahr="" or IsNumeric(fahr)=false then return "Error"
return ((((fahr) - 32) / 9) * 5)
end function
<WebMethod()> Public Function CelsiusToFahrenheit
(ByVal Celsius As String) As String
dim cel
cel=trim(replace(Celsius,",","."))
if cel="" or IsNumeric(cel)=false then return "Error"
return ((((cel) * 9) / 5) + 32)
end function
end class
|
This document is a .asmx file. This is the ASP.NET file extension for XML Web
Services.
To run this example you will need a .NET server.
The first line in this document that it is a Web Service, written in VBScript and the
class name is "TempConvert":
<%@ WebService Language="VBScript" Class="TempConvert" %>
|
The next lines imports the namespace "System.Web.Services" from the .NET
framework.
Imports System
Imports System.Web.Services
|
The next line defines that the "TempConvert" class is a WebService
class type:
Public Class TempConvert :Inherits WebService
|
The next step is basic VB programming. This application has two functions.
One to convert from Fahrenheit to Celsius, and one to convert from Celsius to
Fahrenheit.
The only difference from a normal
application is that this function is defined as a "WebMethod".
Use "WebMethod"
to mark the functions in your application that you would like to make into web
services.
<WebMethod()> Public Function FahrenheitToCelsius
(ByVal Fahrenheit As String) As String
dim fahr
fahr=trim(replace(Fahrenheit,",","."))
if fahr="" or IsNumeric(fahr)=false then return "Error"
return ((((fahr) - 32) / 9) * 5)
end function
<WebMethod()> Public Function CelsiusToFahrenheit
(ByVal Celsius As String) As String
dim cel
cel=trim(replace(Celsius,",","."))
if cel="" or IsNumeric(cel)=false then return "Error"
return ((((cel) * 9) / 5) + 32)
end function
|
The last thing to do is to end the class:
If you save this as an .asmx file and publish it on a server with .NET
support, you should have your first working Web Service. Like our
example Web Service
ASP.NET automates the process
With ASP.NET you do not have to write your own WSDL and SOAP documents.
If you look closer on our example Web Service. You will see that the ASP.NET
has automatically created a
WSDL and
SOAP
request.
|