在使用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>
