Web Services Examples

Any application can have a Web Service component.

The creation and programming of Web Services are independent of the type of programming language.

An example: ASP.NET Web Service

In this example, we will use ASP.NET to create a simple Web Service.

<%@ WebService Language="VB" Class="TempConvert" %>
Imports System
Imports System.Web.Services
Public Class TempConvert : Inherits WebService
<WebMethod()> Public Function FahrenheitToCelsius
(ByVal Fahrenheit As Int16) As Int16
	Dim celsius As Int16 
	celsius = (((Fahrenheit) - 32) / 9) * 5 
	Return celsius
End Function
<WebMethod()> Public Function CelsiusToFahrenheit
(ByVal Celsius As Int16) As Int16
	Dim fahrenheit As Int16
	fahrenheit = (((Celsius) * 9) / 5) + 32 
	Return fahrenheit
End Function
End Class

This document is an .asmx file. It is the ASP.NET file extension used for XML Web Services.

To run this example, we need a .NET server

The first line of this document indicates that it is a Web Service written in VB, with the class name "TempConvert".

<%@ WebService Language="VB" Class="TempConvert" %>

The following code lines import the namespace "System.Web.Services" from the .NET framework.

Imports System
Imports System.Web.Services

The following line defines the "TempConvert" class as a WebSerivce class:

Public Class TempConvert : Inherits WebService

The next steps are basic VB programming. This application has two functions. One converts Fahrenheit to Celsius, and the other converts Celsius to Fahrenheit.

The only difference from a regular application is that this function is defined as "WebMethod".

Please use "WebMethod" to mark the function in the application where you want it to become a web service.

<WebMethod()> Public Function FahrenheitToCelsius
(ByVal Fahrenheit As Int16) As Int16
	Dim celsius As Int16 
	celsius = (((Fahrenheit) - 32) / 9) * 5 
	Return celsius
End Function
<WebMethod()> Public Function CelsiusToFahrenheit
(ByVal Celsius As Int16) As Int16
	Dim fahrenheit As Int16
	fahrenheit = (((Celsius) * 9) / 5) + 32 
	Return fahrenheit
End Function

The last thing to do is to terminate the function and class:

End Function
End Class

If you save this file as a .asmx file and publish it on a server that supports .NET, then you have your first working Web Service.

Automated Handling by ASP.NET

With ASP.NET, you do not need to write WSDL and SOAP documents manually.

If you study our example carefully, you will find that ASP.NET will automatically create WSDL and SOAP requests.