Partager via


Creating a Console Application Client

Creating a console application that acts as an XML Web service client is fairly simple. Once a proxy class is created, a new instance of the proxy class can be created as long as it accessible by the console application. The easiest way to make it accessible is to compile the proxy class into the assembly for the console application. Alternatively, the proxy class can be compiled into an assembly and deployed where the console application can access it.

To create an XML Web service console client application

  1. Create a proxy for the XML Web service.

    Wsdl https://www.contoso.com/Counter.asmx?WSDL
    [Visual Basic]
    Wsdl /language:VB https://www.contoso.com/Counter.asmx?WSDL
    

    For more information, see Creating an XML Web Service Proxy.

  2. Create a console application.

  3. Create an instance of the proxy class in your client code.

    Counter myCounter = new Counter();
    [Visual Basic]
    Dim myCounter As New Counter()
    
  4. Call the method of the proxy class that communicates with your XML Web service method.

    UsageCount = counter.ServiceUsage();
    [Visual Basic]
    UsageCount = counter.ServiceUsage()
    
  5. Compile the console application into an executable. In the following example, the console application was saved as 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
    

The following code example demonstrates a console application client for the XML Web service above.

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

See Also

Building XML Web Service Clients | Creating Clients for XML Web Services