Web Services Examples

Elke applicatie kan Web Service-componenten hebben.

Het maken en programmeren van Web Services is onafhankelijk van het type programmeertaal.

Een voorbeeld: ASP.NET Web Service

In dit voorbeeld zullen we een eenvoudige Web Service maken met ASP.NET.

<%@ 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

Dit document is een .asmx-bestand. Dit is de ASP.NET-bestandsextensie voor XML Web Services.

Om dit voorbeeld te laten draaien, hebben we een .NET-server nodig

De eerste regel van dit document geeft aan dat dit een Web Service is, geschreven in VB, met de class naam "TempConvert".

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

De volgende code regel importeert de naamruimte "System.Web.Services" van het .NET Framework.

Imports System
Imports System.Web.Services

De volgende regel definieert "TempConvert" als een WebSerivce-klasse:

Public Class TempConvert : Inherits WebService

De volgende stappen zijn basis VB-programmeren. Deze applicatie heeft twee functies. Één zet Fahrenheit om naar Celsius en de andere omgekeerd.

Het enige verschil met een normale applicatie is dat deze functie is gedefinieerd als "WebMethod".

Gebruik "WebMethod" om functies te markeren in de applicatie die u wilt omzetten naar web services.

<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.

ASP.NET Automation

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

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