共用方式為


HOW TO:建立主控台應用程式用戶端

本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.

程式碼範例

建立主控台應用程式做為 Web 服務用戶端的方法相當簡單。一旦建立 Proxy 類別,只要主控台應用程式可存取它,就會建立 Proxy 類別的新執行個體。讓它可供存取的最簡單方法是,將 Proxy 類別編譯為主控台應用程式的組件。或者,Proxy 類別也可以先編譯為組件,再部署至主控台應用程式可存取的位置。

若要建立 Web 服務主控台用戶端應用程式

  1. 建立 Web 服務的 Proxy。

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

    如需詳細資訊,請參閱建立 XML Web Service Proxy

  2. 建立主控台應用程式。

  3. 在用戶端程式碼中建立 Proxy 類別的執行個體。

    Counter myCounter = new Counter();
    
    Dim myCounter As New Counter()
    
  4. 呼叫 Proxy 類別中與 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 Service 用戶端

其他資源

建立 XML Web Service 的用戶端