How to: Create a Console Application Client

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation.

Code Example

Creating a console application that acts as a 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 is 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 a Web service console client application

  1. Create a proxy for the Web service.

    Wsdl https://www.contoso.com/Counter.asmx?WSDL
    
    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();
    
    Dim myCounter As New Counter()
    
  4. Call the method of the proxy class that communicates with your Web service method.

    UsageCount = counter.ServiceUsage();
    
    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
    
    vbc /t:exe /r:System.dll,System.Web.dll,System.XML.dll,System.Web.Services.dll UsageMonitor.vb Counter.vb
    

Example

 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

See Also

Concepts

Building XML Web Service Clients

Other Resources

Creating Clients for XML Web Services