Web Services 例

どんなアプリケーションでも Web Service コンポーネントを持つことができます。

Web Servicesの作成やプログラミング言語の種類に関係ありません。

例:ASP.NET Web Service

この例では、ASP.NETを使用してシンプルな 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

このドキュメントは .asmx ファイルです。これは XML Web Services 用の ASP.NET ファイルエクステンションです。

この例を実行するには、.NET サーバが必要です。

このドキュメントの最初の行は、これは Web Serviceであり、VBで書かれ、class名が "TempConvert" であることを示しています。

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

次のコード行は、.NET フレームワークから "System.Web.Services" という名前空間をインポートしています。

Imports System
Imports System.Web.Services

以下の行は、"TempConvert" クラスが WebSerivce クラスであることを定義しています:

Public Class TempConvert : Inherits WebService

次のステップは基本的な VB プログラミングです。このアプリケーションには、華氏度を摂氏度に変換する関数と、摂氏度を華氏度に変換する関数の2つがあります。

一般的なアプリケーションとの唯一の違いは、この関数が "WebMethod" として定義されていることです。

ウェブサービスになるアプリケーションで "WebMethod" を使用して関数をマークしてください。

<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 Function
End Class

このファイルを.asmxファイルとして保存し、.NETをサポートするサーバーに公開すると、最初の動作するWeb Serviceが手に入ります。

ASP.NETの自動処理

ASP.NETを使用すると、WSDLとSOAPドキュメントを手動で書く必要はありません。

この例を詳しく研究すると、ASP.NETが自動的にWSDLとSOAPリクエストを作成するのに気づくでしょう。