nonepassby

导航

Wcf配置系列1--basicHttpBinding

本篇开始讲述Wcf配置系统(本系列将以微软提供案例来验证),绑定是Wcf中的重要配置,绑定可指定在与终结点通话时所使用的通信机制,并指示如何连接到终结点。

 

绑定包含以下元素:
协议堆栈确定用于发送到终结点的消息的安全性、可靠性和上下文流设置。
传输确定将消息发送到终结点时使用的基础传输协议,例如 TCP 或 HTTP。
编码确定用于发送到终结点的消息的网络编码,例如,文本/XML、二进制或消息传输优化机制 (MTOM)。

 

Wcf框架内置了以下多种绑定:

BasicHttpBinding
WSHttpBinding
WSDualHttpBinding
WSFederationHttpBinding
NetTcpBinding
NetNamedPipeBinding
NetMsmqBinding
NetPeerTcpBinding
MsmqIntegrationBinding
BasicHttpContextBinding
NetTcpContextBinding
WSHttpContextBinding

 

如果系统内置的绑定不能满足要求,可以自定义绑定来进行更灵活的配置,本篇文章介绍BasicHttpBinding。

先看服务器端的配置(web.config):

 

  <system.serviceModel>
    <bindings>
      <!--  Following is the expanded configuration section for a BasicHttpBinding.
            Each property is configured with the default value.
            See the TransportSecurity, and MessageSecurity samples in the
            Basic directory to learn how to configure these features.  
-->
      <basicHttpBinding>
        <binding closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <security mode="None"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

如果只是使用basicHttpBinding的默认行为,则在配置文件中只需要定义binding 节点即可,如果需要自定义basicHttpBinding并且要改变一些设置。则需要定义一个命令的binding配置,并且相应服务的endpoint节点 必须通过bindingConfiguration 属性指向此binding的名称。则相应配置可能如下所示:

 

<services>
    <service 
        
type="Microsoft.ServiceModel.Samples.CalculatorService"
        behaviorConfiguration
="CalculatorServiceBehavior">
       <endpoint address=""
             binding
="basicHttpBinding"
             bindingConfiguration
="Binding1" 
             contract
="Microsoft.ServiceModel.Samples.ICalculator" />
    </service>
</services>

在上面的示例中,bindingConfiguration命名为"Binding1" ,binding配置代码如下:

<bindings>
   <basicHttpBinding>
      <binding name="Binding1" 
               hostNameComparisonMode
="StrongWildcard" 
               receiveTimeout
="00:10:00"
               sendTimeout
="00:10:00"
               openTimeout
="00:10:00"
               closeTimeout
="00:10:00"
               maxMessageSize
="65536" 
               maxBufferSize
="65536" 
               maxBufferPoolSize
="524288" 
               transferMode
="Buffered" 
               messageEncoding
="Text" 
               textEncoding
="utf-8"
               bypassProxyOnLocal
="false"
               useDefaultWebProxy
="true" >
         <security mode="None" />
      </binding>
   </basicHttpBinding>
</bindings>

 

客户端配置:

 

<system.serviceModel>

    <client>
      <endpoint name="" address="http://localhost:3066/service.svc" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="Microsoft.Samples.Http.ICalculator"/>
    </client>

    <bindings>
      <basicHttpBinding>
        <!-- See this sample's service configuration for the expanded 
             configuration section for a basicHttpBinding.  The client binding
             configuration also has an allowCookies setting which is false by default
            
-->
        <binding name="Binding1" allowCookies="false"/>
      </basicHttpBinding>
    </bindings>

  </system.serviceModel>

 

案例下载

 

posted on 2012-07-02 15:50  jack  阅读(917)  评论(0)    收藏  举报