Udostępnij za pośrednictwem


How to: Specify a Service Binding in Code

In this example, an ICalculator contract is defined for a calculator service, the service is implemented in the CalculatorService class, and then its endpoint is defined in code, where it is specified that the service must use the BasicHttpBinding class.

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.

For a description of how to configure this service using configuration elements instead of code, see How to: Specify a Service Binding in Configuration.

To specify in code to use the BasicHttpBinding for the service

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

      <ServiceContract()> _
    Public Interface ICalculator
    
          <OperationContract()> _
          Function Add(ByVal n1 As Double, _
                       ByVal n2 As Double) As Double
    
          <OperationContract()> _
          Function Subtract(ByVal n1 As Double, ByVal _
                            n2 As Double) As Double
    
          <OperationContract()> _
          Function Multiply(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double
    
          <OperationContract()> _
          Function Divide(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double
    
      End Interface
    
    [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
        Implements ICalculator
    
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function
    
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function
    
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function
    
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    
    End 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;
       }
    } 
    
  3. In the hosting application, create the base address for the service and the binding to use with the service.

    ' Specify a base address for the service
    Dim baseAddress = "https://localhost/CalculatorService"
    ' Create the binding to be used by the service.
    
    Dim binding1 As New BasicHttpBinding()
    
    // Specify a base address for the service
    
    String baseAddress = "https://localhost/CalculatorService";
    // Create the binding to be used by the service.
    
    BasicHttpBinding binding1 = new BasicHttpBinding();
    
  4. Create the host for the service, add the endpoint, and then open the host.

    
    Using host As New ServiceHost(GetType(CalculatorService))
        With host
            .AddServiceEndpoint(GetType(ICalculator), _
                                    binding1, _
                                    baseAddress)
    
            host.Open()
        End With
    End Using
    
    
    using(ServiceHost host = new ServiceHost(typeof(CalculatorService)))
    {
        host.AddServiceEndpoint(typeof(ICalculator),binding1, baseAddress);
    
    
    
        host.Open();
    }    
    

To modify the default values of the binding properties

  1. To modify one of the default property values of the BasicHttpBinding class, set the property value on the binding to the new value before creating the host. For example, to change the default open and close timeout values of 1 minute to 2 minutes, use the following.

    Dim modifiedCloseTimeout As New TimeSpan(0, 2, 0)
    binding1.CloseTimeout = modifiedCloseTimeout
    
    
    TimeSpan modifiedCloseTimeout = new TimeSpan(00, 02, 00);
    binding1.CloseTimeout = modifiedCloseTimeout;
    

See Also

Concepts

Using Bindings to Configure Services and Clients
Specifying an Endpoint Address