How to: Specify a Client Binding in Configuration


In this example, a client console application is created to use a calculator service, and the binding for that client is specified declaratively in configuration. The client accesses the CalculatorService, which implements the ICalculator interface, and both the service and the client use the BasicHttpBinding class.

The procedure outlined assumes that the calculator service is running. For information about how to build the service, see How to: Specify a Service Binding in Configuration. It also uses the ServiceModel Metadata Utility Tool (Svcutil.exe) that Windows Communication Foundation (WCF) provides to automatically generate the client components. The tool generates the client code and configuration for accessing the service.

The client is built in two parts. Svcutil.exe generates the ClientCalculator that implements the ICalculator interface. This client application is then constructed by constructing an instance of ClientCalculator.

It is usually the best practice to specify the binding and address information declaratively in configuration rather than imperatively in code. Defining endpoints in code is usually not practical because the bindings and addresses for a deployed service are typically different from those used while the service is being developed. More generally, keeping the binding and addressing information out of the code allows them to change without having to recompile or redeploy the application.

You can perform all of the following configuration steps by using the Configuration Editor Tool (SvcConfigEditor.exe).

For the source copy of this example, see the BasicBinding sample.

Specifying a client binding in configuration

  1. Use Svcutil.exe from the command line to generate code from service metadata.

    Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address> 
  2. The client that is generated contains the ICalculator interface that defines the service contract that the client implementation must satisfy.

  3. The generated client also contains the implementation of the ClientCalculator.

  4. Svcutil.exe also generates the configuration for the client that uses the BasicHttpBinding class. When using Visual Studio, name this file App.config. Note that the address and binding information are not specified anywhere inside the implementation of the service. Also, code does not have to be written to retrieve that information from the configuration file.

  5. Create an instance of the ClientCalculator in an application, and then call the service operations.

    using System;
    using System.ServiceModel;
    namespace Microsoft.ServiceModel.Samples
        //Client implementation code.
        class Client
            static void Main()
                // Create a client with given client endpoint configuration
                CalculatorClient client = new CalculatorClient();
                // Call the Add service operation.
                double value1 = 100.00D;
                double value2 = 15.99D;
                double result = client.Add(value1, value2);
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
                // Call the Subtract service operation.
                value1 = 145.00D;
                value2 = 76.54D;
                result = client.Subtract(value1, value2);
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
                // Call the Multiply service operation.
                value1 = 9.00D;
                value2 = 81.25D;
                result = client.Multiply(value1, value2);
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
                // Call the Divide service operation.
                value1 = 22.00D;
                value2 = 7.00D;
                result = client.Divide(value1, value2);
                Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
                //Closing the client gracefully closes the connection and cleans up resources
                Console.WriteLine("Press <ENTER> to terminate client.");
  6. Compile and run the client.