Export (0) Print
Expand All

Accessing an XML Web Service in Managed Code

Accessing an XML Web service from managed code is a straightforward process. First, you add a Web reference to your project for the XML Web service you wish to access. The Web reference creates a proxy class with methods that serve as proxies for each exposed method of the XML Web service. Next, you add the namespace for the Web reference. Finally, you create an instance of the proxy class and then access the methods of that class as you would the methods of any other class. For more information, see Code Model for Accessing XML Web Services in Managed Code.

By default, adding a Web reference also adds methods to the proxy class for accessing the XML Web service asynchronously. For more information, see Accessing an XML Web Service Asynchronously in Managed Code.

To access an XML Web service in managed code

  1. Create the application from which you want to access an XML Web service. This application could even be another XML Web service.
  2. Add a Web reference for the XML Web service with which your application will interact. For instructions, see Adding and Removing Web References.
  3. Create an instance of the proxy object in your client code where you want to access the XML Web service.
  4. Access the methods of the XML Web service as you would any other component.

    In the example code below, the client application (Application1) is accessing an XML Web service for which it has a Web reference (Converter) that contains a proxy class (Service1), which has a method (ConvertTemperature) for calling the XML Web service. The two lines of code in bold represent the code that is necessary to access the XML Web service.

    ' Visual Basic
    Imports System
    Module Module1
        Sub Main()
            Dim cService As New Converter.Service1()
            Dim dFahrenheit As Double
            Dim dCelsius As Double
            Console.Write("Temperature in degrees Fahrenheit: ")
            dFahrenheit = Convert.ToDouble(Console.ReadLine())
            dCelsius = cService.ConvertTemperature(dFahrenheit)
            Console.Write("Temperature in degrees Celsius: ")
            Console.WriteLine(dCelsius.ToString())
        End Sub
    End Module
    
    // C#
    using System;
    namespace Application1
    {
       class Class1
       {
          static void Main()
          {
             Converter.Service1 cService = new Converter.Service1();
             Console.WriteLine("Temperature in degrees Fahrenheit: ");
             double dFahrenheit = Convert.ToDouble(Console.ReadLine());
             double dCelsius = cService.ConvertTemperature(dFahrenheit);
             Console.Write("Temperature in degrees Celsius: ");
             Console.WriteLine(dCelsius.ToString());
          }
       }
    }
    

See Also

Accessing XML Web Services in Managed Code | Building XML Web Service Clients

Show:
© 2014 Microsoft