Export (0) Print
Expand All

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.

    [ServiceContract]
    public interface ICalculator
    {
       [OperationContract]
       double Add(double n1, double n2);
       [OperationContract]
       double Subtract(double n1, double n2);
       [OperationContract]
       double Multiply(double n1, double n2);
       [OperationContract]
       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;
       }
    } 
    
    
    
    ms733051.note(en-us,VS.90).gifNote:
    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" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name=" CalculatorService" >
            <endpoint 
            <-- 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 -->
            <--Directory.-->
                address="" 
            <--Specify the binding type -->
                binding="wsHttpBinding"
            <--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.  -->
                bindingConfiguration="Binding1"
                contract="ICalculator" />
          </service>
        </services>
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <-- Binding property values can be modified here. -->
              <--See the next procedure. -->
            </binding>
          </wsHttpBinding>
       </bindings>
      </system.serviceModel>
    </configuration>
    
    
  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.

          <wsHttpBinding>
            <binding name="Binding1"
                     closeTimeout="00:02:00"
                     openTimeout="00:02:00">
            </binding>
          </wsHttpBinding>
    

See Also


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

Community Additions

ADD
Show:
© 2014 Microsoft