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这个范围才行

 

最后,只要注意以上几个地方大都应该能够成功了

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