Share via


方法 : コンソール アプリケーション クライアントを作成する

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

コード例

Web サービス クライアントとして機能するコンソール アプリケーションは、非常に簡単に作成できます。プロキシ クラスを作成すると、そのプロキシ クラスにコンソール アプリケーションからアクセスできる場合は、プロキシ クラスの新しいインスタンスを作成できます。プロキシ クラスにアクセスできるようにする最も簡単な方法は、プロキシ クラスをコンパイルしてコンソール アプリケーションのアセンブリを生成することです。また、プロキシ クラスをコンパイルしてアセンブリを生成し、コンソール アプリケーションがアクセスできる場所にそのアセンブリを配置することもできます。

Web サービス コンソール クライアント アプリケーションを作成するには

  1. Web サービスのプロキシを作成します。

    Wsdl https://www.contoso.com/Counter.asmx?WSDL
    
    Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
    

    詳細については、「XML Web サービス プロキシの作成」を参照してください。

  2. コンソール アプリケーションを作成します。

  3. クライアント コードでプロキシ クラスのインスタンスを作成します。

    Counter myCounter = new Counter();
    
    Dim myCounter As New Counter()
    
  4. Web サービス メソッドと通信するプロキシ クラスのメソッドを呼び出します。

    UsageCount = counter.ServiceUsage();
    
    UsageCount = counter.ServiceUsage()
    
  5. コンソール アプリケーションをコンパイルして実行可能ファイルを生成します。コンソール アプリケーションを UsageMonitor として保存する例を次に示します。

    csc /t:exe /r:System.Web.dll,System.XML.dll,System.Web.Services.dll UsageMonitor.cs Counter.cs
    
    vbc /t:exe /r:System.dll,System.Web.dll,System.XML.dll,System.Web.Services.dll UsageMonitor.vb Counter.vb
    

 using System;
class UsageMonitor {
   public static void Main(string[] args) {
     int UsageCount;
     // Create an instance of the Web service class.
     Counter myCounter = new Counter();
     // Call the Web service method ServiceUsage.
     UsageCount = myCounter.ServiceUsage();
     // Output the results to the console.
     if (UsageCount == 1)
       Console.WriteLine("Web service has been utilized >" + UsageCount.ToString() + "< time.");
     else      
       Console.WriteLine("Web service has been utilized >" + UsageCount.ToString() + "< times.");
  }  
}
Imports System
Class UsageMonitor
    Public Shared Sub Main()
        Dim UsageCount As Integer
        ' Create an instance of the Web service class.
        Dim myCounter As New Counter()
        ' Call the Web service method ServiceUsage.
        UsageCount = myCounter.ServiceUsage()
        ' Output the results to the console.
        If UsageCount = 1 Then
            Console.WriteLine("Web service has been utilized >" _
               & UsageCount.ToString() & "< time.")
        Else
            Console.WriteLine("Web service has been utilized >" _
               & UsageCount.ToString() & "< times.")
        End If
    End Sub
End Class

参照

概念

XML Web サービス クライアントの作成

その他のリソース

XML Web サービスのクライアントの作成