Links: Table of Contents | Single HTML | Single PDF

Chapter 15. Accessing Metro Services Using WCF Clients

Table of Contents

15.1. Creating a WCF Client
15.1.1. Prerequisites to Creating the WCF Client
15.1.2. Examining the Client Class
15.1.3. Building and Running the Client

15.1. Creating a WCF Client

The process of creating a WCF C# client to the addnumbers service is similar to that for a Java programming language client. To create a WCF client you will:

  1. Use the svcutil.exe tool to generate the C# proxy class and contracts for accessing the web service.

  2. Create a client program that uses the generated files to make calls to the addnumbers web service.

This section covers the following topics:

15.1.1. Prerequisites to Creating the WCF Client

You must have the following software installed to create the WCF client:

  • Microsoft Windows Software Development Kit (SDK) for July Community Technology Preview

  • Microsoft .NET Framework 3.5 RTM

  • The csclient-enabled-fromjava.zip example bundle, which is availalble for download.

You must also deploy the addnumbers service described in WSIT Example Using a Web Container Without NetBeans IDE. The service is also available for download.

15.1.2. Examining the Client Class

The client class uses a generated proxy class, AddNumbersImpl, to access the web service. The port instance variable stores a reference to the proxy class.

Example 15.1. 

port = new AddNumbersImplClient("AddNumbersImplPort");
...

Then the web service operation addNumbers is called on port:

Example 15.2. 

...
int result = port.addNumbers (number1, number2);
...

The full Client.cs class is as follows:

Example 15.3. 

using System;

class Client {
    static void Main(String[] args) {
        AddNumbersImplClient port = null;
        try {
            port = new AddNumbersImplClient("AddNumbersImplPort");
            int number1 = 10;
            int number2 = 20;

            Console.Write("Adding {0} and {1}. ", number1, number2);
            int result = port.addNumbers (number1, number2);
            Console.WriteLine("Result is {0}.\n\n",result);

            number1 = -10;
            Console.Write("Adding {0} and {1}. ", number1, number2);
            result = port.addNumbers (number1, number2);
            Console.WriteLine("Result is {0}.\n\n",result);
            port.Close();
        } catch (System.ServiceModel.FaultException e) {
            Console.WriteLine("Exception: " + e.Message);
            if (port != null) port.Close();
        }
    }
}

15.1.3. Building and Running the Client

The example bundle contains all the files you need to build and run a WCF client that accesses a Metro web service written in the Java programming language.

The csclient-enabled-fromjava.zip bundle contains the following files:

  • Client.cs, the C# client class

  • build.bat, the build batch file

This section covers the following topics:

15.1.3.1. Generating the Proxy Class and Configuration File

When creating a Java programming language client, you use the wsimport tool to generate the proxy and helper classes used by the client class to access the web service. When creating a WCF client, the svcutil.exe tool provides the same functionality as the wsimport tool. svcutil.exe generates the C# proxy class and contracts for accessing the service from a C# client program.

The example bundle contains a batch file, build.bat, that calls svcutil.exe to generate the proxy class. The command is:

svcutil /config:Client.exe.config http://localhost:8080/wsit-enabled-fromjava/addnumbers?wsdl
15.1.3.1.1. To Build the AddNumbers Client

The example bundle's build.bat file first generates the proxy class and configuration file for the client, then compiles the proxy class, configuration file, and Client.cs client class into the Client.exe executable file.

To run build.bat, do the following.

  1. At a command prompt, navigate to the location where you extracted the example bundle.

  2. If necessary, customize the build.bat file as described in To Customize the build.bat File.

  3. Type the following command:

    build.bat
15.1.3.1.2. To Customize the build.bat File

To customize the build.bat file for your environment, do the following:

  1. Open build.bat in a text editor.

  2. On the first line, type the full path to the svcutil.exe tool. By default, it is installed at C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin.

  3. On the first line, change the WSDL location URL if you did not deploy the addnumbers service to the local machine, or if the service was deployed to a different port than the default 8080 port number.

    For example, the following command (all on one line) sets the host name to testmachine.example.com and the port number to 8081:

    svcutil /config:Client.exe.config http://testmachine.example.com:8081/wsit-enabled-fromjava/addnumbers?wsdl
  4. On line 2, change the location of the csc.exe C# compiler and the System.ServiceModel and System.Runtime.Serialization support DLLs if you installed the .NET 2.0 and 3.0 frameworks to non-default locations.

15.1.3.1.3. To Run the AddNumbers Client

After the client has been built, run the client by following these steps.

  1. At a command prompt, navigate to the location where you extracted the example bundle.

  2. Type the following command:

    Client.exe

    You will see the following output:

    Adding 10 and 20. Result is 30.
    Adding -10 and 20. Exception: Negative numbers can't be added!