コンソール アプリケーション クライアントの作成
XML Web サービス クライアントとして機能するコンソール アプリケーションは、非常に簡単に作成できます。プロキシ クラスを作成すると、そのプロキシ クラスにコンソール アプリケーションがアクセスできる場合は、プロキシ クラスの新しいインスタンスを作成できます。プロキシ クラスにアクセスできるようにする最も簡単な方法は、プロキシ クラスをコンパイルしてコンソール アプリケーションのアセンブリを生成します。また、プロキシ クラスをコンパイルしてアセンブリを生成し、コンソール アプリケーションがアクセスできる場所にそのアセンブリを配置することもできます。
XML Web サービスのコンソール クライアント アプリケーションを作成するには
XML Web サービスに対するプロキシを作成します。
Wsdl https://www.contoso.com/Counter.asmx?WSDL [Visual Basic] Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
詳細については、「XML Web サービス プロキシの作成」を参照してください。
コンソール アプリケーションを作成します。
クライアント コードでプロキシ クラスのインスタンスを作成します。
Counter myCounter = new Counter(); [Visual Basic] Dim myCounter As New Counter()
XML Web サービス メソッドと通信するプロキシ クラスのメソッドを呼び出します。
UsageCount = counter.ServiceUsage(); [Visual Basic] UsageCount = counter.ServiceUsage()
コンソール アプリケーションをコンパイルして実行可能ファイルを生成します。コンソール アプリケーションを
UsageMonitor
として格納する例を次に示します。csc /t:exe /r:System.Web.dll,System.XML.dll,System.Web.Services.dll UsageMonitor.cs Counter.cs [Visual Basic] vbc /t:exe /r:System.dll,System.Web.dll,System.XML.dll,System.Web.Services.dll UsageMonitor.vb Counter.vb
上の XML Web サービスのコンソール アプリケーション クライアントを次のコード例に示します。
using System;
class UsageMonitor {
public static void Main(string[] args) {
int UsageCount;
// Create an instance of the XML Web service class.
Counter myCounter = new Counter();
// Call the XML Web service method ServiceUsage.
UsageCount = myCounter.ServiceUsage();
// Output the results to the console.
if (UsageCount == 1)
Console.WriteLine("XML Web service has been utilized >" + UsageCount.ToString() + "< time.");
else
Console.WriteLine("XML Web service has been utilized >" + UsageCount.ToString() + "< times.");
}
}
[Visual Basic]
Imports System
Class UsageMonitor
Public Shared Sub Main()
Dim UsageCount As Integer
' Create an instance of the XML Web service class.
Dim myCounter As New Counter()
' Call the XML Web service method ServiceUsage.
UsageCount = myCounter.ServiceUsage()
' Output the results to the console.
If UsageCount = 1 Then
Console.WriteLine("XML Web service has been utilized >" _
& UsageCount.ToString() & "< time.")
Else
Console.WriteLine("XML Web service has been utilized >" _
& UsageCount.ToString() & "< times.")
End If
End Sub
End Class