建立主控台應用程式用戶端
建立能夠當作 XML Web Service 用戶端的主控台應用程式相當簡單。一旦建立 Proxy 類別,就可以建立 Proxy 類別的新執行個體,條件是主控台應用程式必須能存取新執行個體。如果要能存取,最簡單的方式是將 Proxy 類別編譯為主控台應用程式的組件。另一個方法是將 Proxy 類別編譯為組件,然後部署在主控台應用程式能存取的位置。
若要建立 XML Web Service 主控台用戶端應用程式
建立 XML Web Service 的 Proxy。
Wsdl https://www.contoso.com/Counter.asmx?WSDL [Visual Basic] Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
如需詳細資訊,請參閱建立 XML Web Service Proxy。
建立主控台應用程式。
在您的用戶端程式碼中,建立 Proxy 類別的執行個體。
Counter myCounter = new Counter(); [Visual Basic] Dim myCounter As New Counter()
呼叫能與您 XML Web Service 方法進行通訊的 Proxy 類別方法。
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 Service 的主控台應用程式用戶端。
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