如何:创建控制台应用程序客户端
本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.
代码示例
创建充当 Web 服务客户端的控制台应用程序非常简单。创建了代理类后,只要控制台应用程序可以访问该代理类,便可创建它的新实例。使该代理类可供访问的最简单方法是将其编译到用于控制台应用程序的程序集中。或者,也可以将该代理类编译到一个程序集中,并部署到控制台应用程序可以访问它的位置。
创建 Web 服务控制台客户端应用程序
创建 Web 服务的代理。
Wsdl https://www.contoso.com/Counter.asmx?WSDL
Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
有关更多信息,请参见创建 XML Web services 代理。
创建一个控制台应用程序。
在客户端代码中创建代理类的一个实例。
Counter myCounter = new Counter();
Dim myCounter As New Counter()
调用与 Web 服务方法进行通信的代理类的方法。
UsageCount = counter.ServiceUsage();
UsageCount = counter.ServiceUsage()
将控制台应用程序编译为可执行文件。在下面的示例中,控制台应用程序保存为
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