如何:在代码中指定服务绑定

在本示例中,将为计算器服务定义一个 ICalculator 协定,在 CalculatorService 类中实现该服务,然后在代码中定义其终结点(在这段代码中还指定该服务必须使用 BasicHttpBinding 类)。

通常,最佳做法是以声明方式在配置中指定绑定和地址信息,而不是在代码中强制指定。在代码中定义终结点通常不可行,因为已部署的服务的绑定和地址通常不同于开发服务时使用的绑定和地址。一般说来,通过将绑定和寻址信息放置在代码之外,无需重新编译或重新部署应用程序即可更改这些信息。

有关如何使用配置元素而不是代码来配置此服务的说明,请参见如何:在配置中指定服务绑定

在代码中指定将 BasicHttpBinding 用于服务

  1. 为该类型的服务定义服务协定。

      <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. 在服务类中实现该服务协定。

    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. 在主机应用程序中,创建该服务的基址以及要用于该服务的绑定。

    ' 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. 创建该服务的宿主,添加终结点,然后打开该宿主。

    
    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();
    }    
    

修改绑定属性的默认值

  1. 若要修改 BasicHttpBinding 类的默认属性值之一,请在创建宿主之前将绑定上的该属性值设置为新值。例如,若要将默认打开和关闭超时值从 1 分钟更改为 2 分钟,请使用下面的代码。

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

另请参见

概念

使用绑定配置服务和客户端
指定终结点地址