XML Web Service Clients

The .NET Framework allows the programmer to create a proxy class that represents an XML Web service. This proxy takes the form of a source file, which can be compiled and used as part of a client application; the class appears as a local type and accesses the XML Web service transparently.

Creating a Proxy Class

Proxy classes are generated using the wsdl.exe tool, included with the .NET Framework and located in the C:Program FilesMicrosoft Visual Studio .NETFrameworkSDKBin folder. The wsdl tool provides the command-line options described in Table 19-6.

Table 19-6. Command-Line Options for the WSDL Tool

Option

Description

/language:<language>

Specifies the language to use when generating the source file. Languages are CS for C#, VB for Visual Basic, and JS for JScript. The default is C#.

/namespace:<namespace>

The namespace directive for the generated class. Defaults to the global namespace.

/out:<filename>

The name of the file to generate. The default name is generated from the WSDL definition.

/protocol:<protocol>

Specifies the protocol that will be used to communicate with the XML Web service. See the earlier section Protocols for more information.

/username:<username>

Specifies authentication details for password-protected XML Web services.

/password:<password>

 

/domain:<domain>

 

/proxy:<url>

Specifies the Internet proxy to be used. Defaults to the system proxy settings.

/proxyusername:<username>

 

/proxypassword:<password>

 

/proxydomain:<domain>

 

The following statement illustrates how to create a proxy for the example SumService XML Web service developed earlier in this chapter, accepting the default settings for the command-line options:

wsdl http://localhost/SumService/SumService.asmx

The statement generates a C# source file named Sum.cs containing a proxy class for the XML Web service.

Proxy classes can be created for XML Web services that haven’t been created with .NET; the URL that is passed to the wsdl.exe tool must point to the WSDL document for the service.

Using a Proxy Class

Using a proxy class is no different from using any other class. The following example demonstrates a class that relies on the proxy just created, which we have saved in a source file named SumClient.cs:

public class SumClient {

    static void Main(string[] p_args) {
        Sum x_sum = new Sum();
        int x_result = x_sum.sumNumbers(10, 20);
        System.Console.WriteLine("Result: " + x_result);
    }
}

The statements related to the proxy class are marked in boldface. We create a new instance of the Sum proxy class and invoke the sumNumbers method. Each of the methods exposed by the XML Web service is represented by a C# method. The client class and the proxy are compiled using the following statement:

csc /out:SumClient.exe SumClient.cs Sum.cs

This statement creates an executable named SumClient.exe, which will automatically invoke the XML Web service as required.

More Info

For more information about using the C# compiler, see Chapter 4.

Executing the SumClient application will generate the following output:

Result: 30

Asynchronous Programming

For each XML Web service method, three methods are automatically created in the proxy class, as described in Table 19-7.

Table 19-7. The XML Web Service Proxy Class Methods

Proxy Class Method

Description

<Web Service Method Name>

Sends a message to the XML Web service synchronously.

Begin<Web Service Method Name>

Begins the process of sending a message to the XML Web service asynchronously.

End<Web Service Method Name>

Ends the process of sending an asynchronous message.

Using the synchronous method is shown in the preceding section; this section deals with using XML Web services asynchronously, which follows the general model for all .NET asynchronous features.

The following example shows a client that makes asynchronous use of the example XML Web service:

using System;

public class AsyncSumClient {

    static void Main(string[] p_args) {
        Sum x_sum = new Sum();
        AsyncCallback x_callback =
            new AsyncCallback(AsyncSumClient.SumCallBack);
        x_sum.BeginsumNumbers(10, 20, x_callback, x_sum);

        Console.ReadLine();
    }

    static void SumCallBack(IAsyncResult p_callback) {
        Sum x_sum = (Sum)p_callback.AsyncState;
        int x_result = x_sum.EndsumNumbers(p_callback);
        System.Console.WriteLine("Result: " + x_result);
    }
}

The statements that relate to asynchronous support are marked in boldface. The request is started by calling the BeginsumNumbers method, passing in the arguments to the Web method, a delegate that references the SumCallBack method, and the instance of the proxy class that is being used. When the request has completed, the SumCallBack method is invoked, and we use the AsyncState property of the IAsyncResult to obtain the instance of the proxy class; the results of the request are obtained by calling the EndsumNumbers method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset