高并发WCF配置

在使用WCF做服务接口时,TCP模式肯定比Http效率高,Binary/MTOM格式的绑定也会Text格式的绑定高效。

两个endpoint,一个用来调试:ms-mex的binding是用来方便WCF调试工具和远程对象应用,另一个是实际工作模式配置:customBinding。

为了提高接口的并发数,特实验custonBinding各个参数对并发的影响,以下配置是目前实验tps较高的参数配置:

  <system.serviceModel>
    <services>
      <service name="GW.Stone.BLL.Wcf.StoneService" behaviorConfiguration="NetTcpBehavior">
        <!--自定义契约,高并发单机约1800tps,生产用配置-->
        <endpoint address="net.tcp://127.0.0.1:50061/GWStoneService" binding="customBinding" contract="GW.Stone.BLL.Wcf.IStoneService" bindingConfiguration="MySessionBinding">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        
        <!--ms-mex标准契约,并发单机约1000tps,远程服务引用及wcf测试工具调试配置。
        此处mex是个壳配置,客户端代理proxy类在添加服务器引用时,VS会自动生成对自定义契约高并发配置的app.config配置文件。-->
        <endpoint address="net.tcp://127.0.0.1:50062/GWStoneService" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
    </services>

    <!--behavior 元素包含服务行为的设置集合。-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="NetTcpBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <!--是否在错误中包含有关异常的详细信息-->
          <serviceDebug includeExceptionDetailInFaults="True" />
           
          <!--          
          使用 ServiceThrottlingBehavior 类可控制各种吞吐量设置,这些设置可以让您优化服务性能,以帮助避免应用程序内存不足。
          http://msdn.microsoft.com/zh-cn/library/system.servicemodel.description.servicethrottlingbehavior(v=vs.110).aspx
          MaxConcurrentCalls 属性可限制当前在整个 ServiceHost 中处理的消息数目。默认为处理器计数的 16 倍。 
          MaxConcurrentInstances 属性可限制在整个 ServiceHost 中一次执行的 InstanceContext 对象数。默认为 MaxConcurrentSessions 的值和 MaxConcurrentCalls 值的总和。
          MaxConcurrentSessions 属性可限制 ServiceHost 对象可以接受的会话数。服务主机可接受的最大会话数。 默认为处理器计数的 100 倍。 
          因为运行时负载平衡需要运行应用程序的经验,所以,通过应用程序配置文件使用 ServiceThrottlingBehavior 是修改执行过程以获得最佳服务性能的最常用方法。
          配置文件中使用 <serviceThrottling> 元素来设置此属性的值。
          -->
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <customBinding>
        <!--WSHttpBindingBase 属性
        http://msdn.microsoft.com/zh-cn/library/vstudio/System.ServiceModel.WSHttpBindingBase_properties(v=vs.100).aspx
        -->
        <binding name="MySessionBinding">
          <!--transactionFlow 获取或设置一个值,该值指示此绑定是否应支持流动 WS-Transactions。
          如果支持事务的流动,则为 true;否则为 false。默认值为 false。-->
          <transactionFlow />
          
          <!--binaryMessageEncoding 定义一个在网络上以二进制形式对 Windows Communication Foundation (WCF) 消息进行编码的二进制消息编码器。
          http://msdn.microsoft.com/zh-cn/library/ms731780(v=vs.110).aspx
          -->
          <binaryMessageEncoding  />
          
          <!--reliableSession 获取一个对象,当使用系统提供的一个绑定时,该对象可提供对可用的可靠会话绑定元素属性的便捷访问。
          http://msdn.microsoft.com/zh-cn/library/System.ServiceModel.Channels.ReliableSessionBindingElement(v=vs.110).aspx
          maxPendingChannels 可靠会话期间可为挂起状态的最大通道数。可为挂起状态的最大通道数。 默认值为 4。 设置的值小于或等于零,或者大于 16384。 
          通道在等待被接受时处于挂起状态。 一旦达到该限制,就不会创建任何通道并将其置于挂起模式,直到此数值降低(通过接受挂起的通道)。
          这是对每个侦听器的限制。当达到此阈值时如果远程应用程序尝试建立新的可靠会话,则会拒绝请求且打开操作将提示此错误。
          -->
          <reliableSession  maxPendingChannels="20"/>
          
          <!--tcpTransport 定义通道用于传输自定义绑定消息的 TCP 传输。
          http://msdn.microsoft.com/zh-cn/library/ms731366(v=vs.110).aspx
          listenBacklog 可为 Web 服务挂起的最大排队连接请求数。 connectionLeaseTimeout 属性限制客户端在引发连接异常之前将等待连接的持续时间。
            这是一个套接字级别属性,控制可能为 Web 服务挂起的最大排队连接请求数。
            ListenBacklog 太低时,WCF 将停止接受请求,并因此删除新连接,直到服务器承认一些现有队列连接。默认值为 16 * 处理器数。
          maxPendingConnections 获取或设置此共享服务的最大挂起连接数,默认值为 100。
          maxPendingAccepts 获取或设置共享服务侦听终结点上的最大未完成并发接受线程数。 默认值为 2-->
          <tcpTransport  listenBacklog="400" maxPendingConnections="1000" maxPendingAccepts="10" />
        </binding>
      </customBinding>
    </bindings>
  </system.serviceModel>

 20190905更新:

service配置,配置超时和消息超配额的问题,"已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。"

  <system.serviceModel>
    <services>
      <service name="Service"  behaviorConfiguration="myNetTcpBehavior">
        <endpoint address="net.tcp://localhost:50200/ServiceApi" binding="netTcpBinding" contract="IService" bindingConfiguration="myNetTcpBindingConfig">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="net.tcp://localhost:50201/ServiceApi" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="myNetTcpBindingConfig" 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="100" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="100" maxReceivedMessageSize ="2147483647">
          <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:30:00" enabled="false"  />
          <security mode="None"  >
            <transport protectionLevel="None" clientCredentialType="None"  />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myNetTcpBehavior">
          <serviceMetadata httpGetEnabled="False" httpGetUrl="http://localhost:8021/ServiceApi" />
          <serviceDebug includeExceptionDetailInFaults="True" />
          <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="10000" maxConcurrentSessions="10000" />
          <bufferedReceive maxPendingMessagesPerChannel="2147483647" />
          <dataContractSerializer maxItemsInObjectGraph="65536000" />
          <serviceTimeouts transactionTimeout ="00:30:00"/>
          <sendMessageChannelCache allowUnsafeCaching="false"/>          
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

  web端配置:

    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IService" 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="100" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="100" maxReceivedMessageSize ="2147483647">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:50200/ServiceApi" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService" contract="ServiceReference.IService" name="NetTcpBinding_IService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

  

原文地址:https://www.cnblogs.com/dreign/p/WCF.html