让IIS支持WCF的netTcpBinding

来源:互联网 发布:数据分析职称 编辑:程序博客网 时间:2024/05/19 08:02

使用WCF的时间不是很长,但是感觉他博大精深,将网络通信的复杂完全隐藏在一个配置文件和接口中。
因为我搞过SOCKET通信模块的编写,对此深有体会!

问题来源,IIS原生支持HTTP协议,不需要做过多的配置就能够让使用这个协议的WCF接口工作,具体协议为basicHttpBinding,wsHttpBinding。但是在测试的过程当中发现netTcpBinding协议的传输速度是basicHttpBinding的三倍以上,所以想使用netTcpBinding协议作为WCF接口的数据传输途径。

但是要让IIS支持WCF的netTcpBinding比较费力,在研究了数天之后总算得到了解决方案。

Web.config具体配置如下:

<?xml version="1.0"?><configuration>  <system.web>    <compilation debug="true" targetFramework="4.0"/>    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>  </system.web>  <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior name="TempBehavior">          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->          <serviceMetadata httpGetEnabled="true"/>          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->          <serviceDebug includeExceptionDetailInFaults="false"/>          <!--设置连接数量-->          <serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentInstances="2147483647" maxConcurrentSessions="2147483647"/>          <!--安全验证-->          <!--<serviceCredentials>            <clientCertificate>              <authentication certificateValidationMode="None"/>            </clientCertificate>          </serviceCredentials>-->        </behavior>      </serviceBehaviors>    </behaviors>    <!--接收数据大小,加密设置:transport,默认安全级别为加密和完整性验证-->    <bindings>      <netTcpBinding>        <binding name="TempNetTcpBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">          <security mode="None">            <transport clientCredentialType="Windows" protectionLevel="None"/>            <message clientCredentialType="Windows"/>          </security>        </binding>      </netTcpBinding>      <!--<wsHttpBinding>        <binding name="TempWsHttpBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">          <security mode="None">            <transport clientCredentialType="Windows"/>            <message clientCredentialType="Windows"/>          </security>        </binding>      </wsHttpBinding>-->    </bindings>    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>    <services>      <service behaviorConfiguration="TempBehavior" name="TempWcf.Server.ServiceTemp">        <endpoint address="" binding="netTcpBinding" bindingConfiguration="TempNetTcpBinding" contract="TempWcf.Server.IServiceTemp"/>        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>      </service>      <!--<service behaviorConfiguration="TempBehavior" name="TempWcf.Server.ServiceTemp">        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TempWsHttpBinding"  contract="TempWcf.Server.IServiceTemp" />      </service>-->    </services>  </system.serviceModel>  <system.webServer>    <modules runAllManagedModulesForAllRequests="true"/>  </system.webServer></configuration>

上图就是我的配置文件,关键代码有两处,一处是:

    <services>      <service behaviorConfiguration="TempBehavior" name="TempWcf.Server.ServiceTemp">        <endpoint address="" binding="netTcpBinding" bindingConfiguration="TempNetTcpBinding" contract="TempWcf.Server.IServiceTemp"/>        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>      </service>

上面的代码规定了IIS配置使用NET.TCP协议;

另外一处是:

          <security mode="None">            <transport clientCredentialType="Windows" protectionLevel="None"/>            <message clientCredentialType="Windows"/>          </security>

上面的代码是安全协议;
上面两处中任何一处配置不对都会导致netTcpBinding协议不能正确运行。

其二,需要配置IIS支持net.tcp协议

具体设置如下:

让IIS支持WCF NET.TCP协议的配置

经过以上两步的配置,就可以让IIS支持WCF的netTcpBinding了。