This documentation is archived and is not being maintained.

How to: Specify a Service Binding in Configuration

In this example, an ICalculator contract is defined for a basic calculator service, the service is implemented in the CalculatorService class, and then its endpoint is configured in the Web.config file, where it is specified that the service uses the BasicHttpBinding. For a description of how to configure this service using code instead of a configuration, see How to: Specify a Service Binding in Code.

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.

All of the following configuration steps can be undertaken using the Configuration Editor Tool (SvcConfigEditor.exe).

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

To specify the BasicHttpBinding to use to configure the service

  1. Define a service contract for the type of service.

    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);
  2. Implement the service contract in a service class.

    public class CalculatorService : ICalculator
       public double Add(double n1, double n2)
          return n1 + n2;
       public double Subtract(double n1, double n2)
          return n1 - n2;
       public double Multiply(double n1, double n2)
          return n1 * n2;
       public double Divide(double n1, double n2)
          return n1 / n2;
    Address or binding information is not specified inside the implementation of the service. Also, code does not have to be written to fetch that information from the configuration file.

  3. Create a Web.config file to configure an endpoint for the CalculatorService that uses the WSHttpBinding.

    <?xml version="1.0" encoding="utf-8" ?>
          <service name=" CalculatorService" >
            <-- Leave the address blank to be populated by default-->
            <--from the hosting environment,in this case IIS, so -->
            <-- the address will just be that of the IIS Virtual -->
            <--Specify the binding type -->
            <--Specify the binding configuration name for that -->
            <--binding type. This is optional but useful if you  -->
            <--want to modify the properties of the binding. -->
            <--The bindingConfiguration name Binding1 is defined  -->
            <--below in the bindings element.  -->
                contract="ICalculator" />
            <binding name="Binding1">
              <-- Binding property values can be modified here. -->
              <--See the next procedure. -->
  4. Create a Service.svc file that contains the following line and place it in your Internet Information Services (IIS) virtual directory.

    <%@ServiceHost language=c# Service="CalculatorService" %> 

To modify the default values of the binding properties

  1. To modify one of the default property values of the WSHttpBinding, create a new binding configuration name - <binding name="Binding1"> - within the <wsHttpBinding> element and set the new values for the attributes of the binding in this binding element. For example, to change the default open and close timeout values of 1 minute to 2 minutes, add the following to the configuration file.

            <binding name="Binding1"

See Also

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