Export (0) Print
Expand All

How to: Specify a Client Binding in Code

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

This procedure assumes that the calculator service is running. For information about building the service, see How to: Specify a Service Binding in Configuration. It also uses the ServiceModel Metadata Utility Tool (Svcutil.exe) Windows Communication Foundation (WCF) provides to automatically generate the client components. The tool generates the client code 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 and then specifying the binding and the address for the service in code.

For the source copy of this example, see the Basic Binding sample.

To specify a custom binding in code

  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.

    public interface ICalculator
       double Add(double n1, double n2);
       double Subtract(double n1, double n2);
       double Multiply(double n1, double n2);
       double Divide(double n1, double n2);
  3. The generated client also contains the implementation of the ClientCalculator.

    public class CalculatorClient : System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>, Microsoft.ServiceModel.Samples.ICalculator
        public CalculatorClient()
        public CalculatorClient(string endpointConfigurationName) : 
        public CalculatorClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        public CalculatorClient(string endpointConfigurationName, EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        public CalculatorClient(Binding binding, EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        public double Add(double n1, double n2)
            return base.Channel.Add(n1, n2);
        public double Subtract(double n1, double n2)
            return base.Channel.Subtract(n1, n2);
        public double Multiply(double n1, double n2)
            return base.Channel.Multiply(n1, n2);
        public double Divide(double n1, double n2)
            return base.Channel.Divide(n1, n2);
  4. Create an instance of the ClientCalculator that uses the BasicHttpBinding class in a client application, and then call the service operations at the specified address.

        //Client implementation code.
        class Client
            static void Main()
                //Specify the binding to be used for the client.
                BasicHttpBinding binding = new BasicHttpBinding();
                //Specify the address to be used for the client.
                EndpointAddress address = 
                   new EndpointAddress("http://localhost/servicemodelsamples/service.svc");
                // Create a client that is configured with this address and binding.
                CalculatorClient client = new CalculatorClient(binding, address);
                // 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.");
  5. Compile and run the client.

See Also

© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21

Community Additions

© 2014 Microsoft