Web Services Examples
- Previous Page WS Platform
- Next Page WS Usage
Anumang aplikasyon ay may kakayahan na magkaroon ng komponente na Web Service.
Ang paglikha at pag-programming ng Web Services ay hindi nauugnay sa uri ng programming language.
Isang halimbawa: ASP.NET Web Service
Sa halimbawa na ito, gagamitin namin ang ASP.NET upang bumuo ng isang 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
Ang dokumento na ito ay isang .asmx file. Ito ay isang ekspansyong file na ginagamit sa XML Web Services sa ASP.NET.
Upang pataasin ang halimbawa na ito, kailangan namin ng isang .NET server
Ang unang linya ng dokumento na ito ay nagpapahiwatig na ito ay isang Web Service, na ginawa sa VB, na may pangalan na "TempConvert".
<%@ WebService Language="VB" Class="TempConvert" %>
Ang sumusunod na linya ng code ay nag-import ng namespace "System.Web.Services" mula sa .NET framework.
Imports System Imports System.Web.Services
Ang sumusunod na linya ng code ay nagtatalaga ng "TempConvert" class bilang isang WebSerivce class:
Public Class TempConvert :Inherits WebService
Ang susunod na hakbang ay ang batayang programming sa VB. Ang aplikasyon na ito ay may dalawang function: isa ay nag-convert mula sa Fahrenheit sa Celsius, at ang iba'y mula sa Celsius sa Fahrenheit.
Ang tanging pagkakaiba ng ganitong function ay ito ay tinatawag na "WebMethod".
Mga aplikasyon na gusto mong maging web services, gamitin ang "WebMethod" upang tandaan ang function.
<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
Ang huling bagay na dapat gawin ay tapusin ang function at class:
End Function End Class
Kung isasave mo ang file na ito bilang .asmx file at ipapalabas sa server na sumusuporta sa .NET, magkakaroon ka ng iyong unang gumagana na Web Service.
Automation of ASP.NET
Sa pamamagitan ng ASP.NET, hindi mo kailangang magpatala ng WSDL at SOAP documents sa sarili mo.
Kung sinasaliksik mo ang aming katulad na halimbawa, makikita mo na ang ASP.NET ay magpapalit nang awtomatiko ang WSDL at SOAP request.
- Previous Page WS Platform
- Next Page WS Usage