配置WCF的心得

经过一整天的折腾,总算对手动配置WCF有些感觉了,于是写篇博文记录一下心得。
根据蒋金楠老师的博文所说的, WCF的终结点有三个要素组成,分别是地址(Address)、绑定(Binding)和契约(Contract),简记可写成Endpoint = ABC。
 地址:地址决定了服务的位置,解决了服务寻址的问题。
 绑定:绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能对消息进行的相应处理。绑定的类型包括BasicHttpBinding、WsHttpBinding、NetTcpBinding等。
 契约:契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。
 以上这些内容摘抄自蒋老师的博文。理解的这些对配置WCF很有帮助。
那下面就一步步来配置一个WCF。
首先是服务端,
一个WCF的核心是终结点,那么先把终结点写列出来,

   <services>
      <service name="BLL.Logic" behaviorConfiguration="te">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9091/logicService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="ws2007HttpBinding" contract="BLL.ILogic"  bindingConfiguration="transportWS2007HttpBinding" />
      </service>
    </services>

    从<endpoint>几个属性address(地址) binding(绑定),Contract(契约),这几个属性正是上面所说的"ABC"  注意一下 binding里填的是BasicHttpBinding、WsHttpBinding、NetTcpBinding这些值,而确切使用哪一个binding呢,就需要在bindingConfiguration中设置,值是使用的<binding>的name值。contract项目中contract的契约接口的完全限定名,这里关于binding的配置接下来会介绍。address没填值,这里在<host>中已经给定了一个地址了。
      介绍完<endpoint>,再看看<endpoint>外面的。<endpoint>包含在<services>的<service>下,这里的<serivces>是一个集合,里面可以包含多个服务,每个服务都会有特定的命名(name),而name则是项目里头实现契约(Contract)的服务(Service)的类的完全限定名。这里对servicebehavior进行了一些设置,具体的内容在名为te的<servicebehavior>中。
既然上面有配置有涉及到binding和behavior,下面则分别对两者进行配置。

<bindings>
      <ws2007HttpBinding>
        <binding name="transportWS2007HttpBinding" maxReceivedMessageSize="2147483647"  maxBufferPoolSize="2147483647">
          <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
          <security mode="Message">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </ws2007HttpBinding>

        <basicHttpBinding>
          <binding name="newBinding" maxBufferPoolSize="21474835647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
            <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          </binding>
        </basicHttpBinding>
</bindings>

     bindings这部分和services一样,也是一个集合,里面包含着各种类型的binding,例如在<ws2007HttpBinding>里面的<binding>才是确切的某一个binding, <endpoint>使用时,bindingConfiguration的名称要写对外,binding的类型也不能错。<binding>里面的子节点和属性就不再一一介绍了,若是要通过WCF传输比较大的数据时,要在binding的属性和<readerQuotas>设置一下。

  <behaviors>
      <serviceBehaviors>
        <behavior name="te">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>


     最后到behaviors了。同理,behaviors也是一个集合,里面有两种类型,一种是serviceBehaviors,用于配置service的;另一种是endpointBehaviors,用于配置endpoint的。这两种类型都是一个集合,子节点<behavior>是它们的子项,以name来区分各个behavior,至于里面有什么属性和子项也不多说了,使用时在相应的service或endpoint的behaviorConfiguration属性填上behavior的name值就行了。
服务端的配置就唠叨到这里,下面到客户端的。

    <client>
      <endpoint address="http://localhost:9091/logicService" binding="ws2007HttpBinding"
          bindingConfiguration="WS2007HttpBinding_ILogic" contract="Proxy.ILogic"
          name="WS2007HttpBinding_ILogic">
      </endpoint>
    </client>

首先也是是终结点,客户端的终结点放在client里,里面也是有"ABC",这里的address一定要与服务端配置的一样,否则找不到相应的服务的。binding的类型也要与服务端的一样,contract则是用svcutil或其他工具生成的代码里的那个类的完全限定名。

      <ws2007HttpBinding>
        <binding name="WS2007HttpBinding_ILogic" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
            messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
            allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="1024" maxArrayLength="2147483647"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
              enabled="false" />
        </binding>
      </ws2007HttpBinding>

另一个还要提的是这个binding,客户端的binding比服务端的要配置多一点东西closeTimeout,openTimeout,receiveTimeout 大致与服务端一样。
另外若要传输比较的大数据时,可以按我这样来配,其实这个配置已经适用于传输几M的图片。由于是个入门者,很多东西的理解还不够透彻,以上有说错的还请各位批评指出。谢谢!

 

posted @ 2013-01-27 10:31  猴健居士  阅读(3311)  评论(12编辑  收藏  举报