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

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

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

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

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

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

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

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

    
    // Specify a base address for the service
    
    String baseAddress = "http://localhost/CalculatorService";
    // Create the binding to be used by the service.
    
    BasicHttpBinding binding1 = new BasicHttpBinding();
    
    
    ' Specify a base address for the service
    Dim baseAddress = "http://localhost/CalculatorService"
    ' Create the binding to be used by the service.
    
    Dim binding1 As New BasicHttpBinding()
    
  4. 创建该服务的宿主,添加终结点,然后打开该宿主。

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

修改绑定属性的默认值

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

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

另请参阅