如何:创建控制台应用程序客户端

代码示例

创建充当 Web 服务客户端的控制台应用程序非常简单。 创建了代理类后,只要控制台应用程序可以访问该代理类,便可创建它的新实例。 使该代理类可供访问的最简单方法是将其编译到用于控制台应用程序的程序集中。 或者,也可以将该代理类编译到一个程序集中,并部署到控制台应用程序可以访问它的位置。

创建 Web 服务控制台客户端应用程序

  1. 创建 Web 服务的代理。

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

    有关更多信息,请参见创建 XML Web services 代理

  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 services 客户端

其他资源

创建 XML Web services 客户端

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。