silverlight 與IIS宿主的net.tcp WCF通訊實現過程

silverlight 與IIS宿主的net.tcp通訊方式據說比其它綁定要快N倍於是決定把以前的HttpBinding改成tcpBinding,但整個過程並不順利有若干地方需要注意才能完成整個工作。爲了便於以後參考特大概回憶記錄如下步驟:

1、服務端的配置文件需要正確的Web.config文件

1)綁定配置

   <netTcpBinding>
    <binding name="netTcpBindConfig"  closeTimeout="00:30:00"
      openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
      transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
      hostNameComparisonMode="StrongWildcard" listenBacklog="10"
      maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
      maxReceivedMessageSize="2147483647">
     <readerQuotas maxDepth="2147483647"
             maxStringContentLength="2147483647"
             maxArrayLength="2147483647"
             maxBytesPerRead="2147483647"
             maxNameTableCharCount="2147483647" />
     <reliableSession ordered="true"  inactivityTimeout="00:01:00" enabled="false" />
     <security mode="None">
      <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"  />
      <message clientCredentialType="Windows"  />
     </security>
    </binding>
   </netTcpBinding>

2)綁定服務端口

   <service behaviorConfiguration="MyBehavior" name="CMIAP.WCF.ZT">
    <host>
     <baseAddresses>
      <add baseAddress="net.tcp://localhost:4503/ZT.svc"/>
     </baseAddresses>
    </host>
    <endpoint address="" binding="netTcpBinding" contract="CMIAP.WCF.ZT" bindingConfiguration="netTcpBindConfig" />
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
   </service>
2、客戶端引用時注意不能直接引用http的地址也不能引用上面baseAddresses的地址,具體地址需要通過部署服務端後在IE裏訪問後得到如下圖所示:

在客戶端引用地址欄輸入上面獲取的net.tcp://後面的地址即可。

3、跨域問題

這個問題非常經典,出現的情況很多,主要解決好跨域文件和位置基本能解決所有問題

出現的錯誤主要是“TCP 錯誤代碼 10013: 試圖以其訪問權限所禁止的方式訪問套接字”

跨域文件clientaccesspolicy.xml必須放到80端口所在路徑下才行,可通過http://localhost/clientaccesspolicy.xml能正常訪問來進行測試

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
        <socket-resource port="4502-4530" protocol="tcp" />
   </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>
注意上面的紅色部分是我一直未能成功之所在,以前的綁定方式不需要這條即能解決跨域問題,但net.tcp則必須加上這條

4、系統配置部分

要實現功能還必須注意系統配置要進行如下設置

需要啓用這個功能

其次,需要安裝IIS6的兼容性和管理工具

5、訪問端口

注意綁定的端口只能在4502-4530這個範圍才行

 

最後,只要注意以上幾個地方大都應該能夠成功了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章