Exemplo de Web Services

Any application can have a Web Service component.

The creation and programming of Web Services are not related to 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 Como Int16) Como Int16
	Dim celsius Como Int16 
	celsius = ((((Fahrenheit) - 32) / 9) * 5) 
	Retornar celsius
Fim da Função
<WebMethod()> Public Function CelsiusToFahrenheit
(ByVal Celsius Como Int16) Como Int16
	Dim fahrenheit Como Int16
	fahrenheit = ((((Celsius) * 9) / 5) + 32) 
	Retornar fahrenheit
Fim da Função
Fim da Classe

This document is an .asmx file. This 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 this 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 that the "TempConvert" class is 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".

Use "WebMethod" to mark the function in the application you want to become web services.

<WebMethod()> Public Function FahrenheitToCelsius
(ByVal Fahrenheit Como Int16) Como Int16
	Dim celsius Como Int16 
	celsius = ((((Fahrenheit) - 32) / 9) * 5) 
	Retornar celsius
Fim da Função
<WebMethod()> Public Function CelsiusToFahrenheit
(ByVal Celsius Como Int16) Como Int16
	Dim fahrenheit Como Int16
	fahrenheit = ((((Celsius) * 9) / 5) + 32) 
	Retornar fahrenheit
Fim da Função

A última coisa a fazer é encerrar a função e a classe:

Fim da Função
Fim da Classe

Se você salvar este arquivo como arquivo .asmx e publicá-lo em um servidor que suporte .NET, você terá seu primeiro Web Service funcional.

Processamento Automático do ASP.NET

Com ASP.NET, você não precisa escrever manualmente os documentos WSDL e SOAP.

Se você estudar nosso exemplo com atenção, você notará que o ASP.NET cria automaticamente WSDL e solicitações SOAP.