如何:在配置中指定服务绑定

在本示例中,为基本计算器服务定义 ICalculator 协定,在 CalculatorService 类中实现该服务,然后在 Web.config 文件中配置其终结点,该文件中指定此服务使用 BasicHttpBinding。有关如何使用代码(而非配置)配置此服务的说明,请参见如何:在代码中指定服务绑定

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

所有下列配置步骤都可以使用配置编辑器工具 (SvcConfigEditor.exe) 来执行。

有关此示例的源副本,请参见BasicBinding

指定要用于配置服务的 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;
       }
    } 
    
    ms733051.note(zh-cn,VS.100).gif注意:
    未在该服务的实现内部指定地址或绑定信息。而且,不必编写码就可以从配置文件中获取该信息。

  3. 创建一个 Web.config 文件来配置使用 WSHttpBindingCalculatorService 的终结点。

    <?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. 创建一个包含下行的 Service.svc 文件,然后将其放到 Internet 信息服务 (IIS) 虚拟目录中。

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

修改绑定属性的默认值

  1. 若要修改 WSHttpBinding 的默认属性值之一,请先在 <wsHttpBinding> 元素中创建一个新的绑定配置名称 (<binding name="Binding1">),然后在此绑定元素中为绑定属性设置新值。例如,若要将默认打开和关闭超时值从 1 分钟更改为 2 分钟,需要将下列内容添加到配置文件中。

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

另请参见

概念

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