今天網(wǎng)上的系統(tǒng)突然報錯,經(jīng)過排查是調(diào)用wcf報錯了,報錯信息以下:
System.TimeoutException: The request channel timed out while waiting for a reply after 00:00:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: The request operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout.
at System.ServiceModel.Channels.ReliableRequestSessionChannel.SyncRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
--- End of inner exception stack trace ---
網(wǎng)上找了半天資料,這篇帖子是很有價值的
I also had this timeout problem. The client requests to my WCF service would work 3 to 6 times, then it would fail with a timeout. I was passing tiny messages back and forth, so I knew "I cannot accomplish such big error with such tiny messages" ;-)
The problem was I was forgetting to call .Close() on the WCF client object! Once I added the appropriate ".Close()", the error disappeared.
So once you're done calling the WCF service, make sure to call Close(), probably in your Finally block.
經(jīng)過排查確切是相干的部門調(diào)了這個服務(wù),沒有加.Close().致使的.
在這個部門沒有修改代碼之前,服務(wù)器的配置如果需要修改,可以增加service的配置連接數(shù)
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<dataContractSerializer maxItemsInObjectGraph="10000000"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="100" maxConcurrentInstances="100" />
</behavior>
</serviceBehaviors>
如果wcf是宿主在iis上的,可以增加iis的全局配置
http://www.cnblogs.com/z2002m/archive/2012/10/26/1918342.html
這篇很有價值了
最近公司有位仁兄寫了1個監(jiān)控IIS的軟件,里面提到1個連接數(shù), 此連接數(shù)主要是搜集WMI信息,
把代碼寫1下:
// 取到IIS的各個站點
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_PerfFormattedData_W3SVC_WebService");
// 每一個站點
foreach (System.Management.ManagementObject obj in mc.GetInstances())
{
// 這里就是每一個站點當前的連接數(shù)
Convert.ToInt64(obj.Properties["CurrentConnections"].Value); //
}
乍1看, 此 CurrentConnections是做甚么用的呢? 由于我1直想了解1個IIS站點默許在同1時刻究竟能接受多少連接要求, 上網(wǎng)查了查,查到了maxconnection。
首先看看微軟是怎樣解釋的:
這個 maxconnection參數(shù)用來肯定: 對每一個IP能容納多少個連接數(shù)(翻譯的其實不1定準)。
<connectionManagement>
<add address="*" maxconnection="2"> // 這里就說明是2個
</connectionManagement>
這個 maxconnection會不會是并發(fā)連接數(shù)呢?
我們來看看 http://www.microsofttranslator.com/BV.aspx?ref=CSSKB&from=en&to=zh-chs&a=http://support.microsoft.com/kb/821268/en-us?fr=1
里面有1段寫的很明白,
請注意在使用此配置時您可以履行的每一個 CPU 12 ASP.NET 要求最多在同1時間由于 100⑻8 = 12。因此,最少 88 * N 工作線程和 88 * N 完成端口線程都可用的其它用處 (例如 Web 服務(wù)回調(diào))。
基本上就明白了。 maxconnection = 12*CPU數(shù)量。 我們公司的服務(wù)器是使用雙核的, 也就是說,在默許的情況下,
我們公司服務(wù)器上的IIS站點的默許最大連接數(shù)是24,
接下來驗證1下, 測試的目的是為了證明: 當超過24個訪問連接候,IIS的站點還能不能接受其他的連接,
實現(xiàn)準備:
1 寫1個WEBService,里面包括A和B方法,其中A方法里面就1句Thread.Sleep(10分鐘), 而B方法則直接Return "OK";
2 VS2008負載測試(摹擬24個用戶同時訪問1臺服務(wù)器上的webServiceA方法,由于A方法是會讓線程休眠10分鐘)
3 同時在服務(wù)器上搜集WMI信息來觀測WEBService當前的連接數(shù), 使用: obj.Properties["CurrentConnections"].Value (具體搜集方法看本文開頭)
這是負載測試,
這是負載測試調(diào)用的測試方法。
OK, 準備就緒, 開吃。。哦,不對, 開始測試,這里我就不貼出來當時測試的圖, 我把我測試的結(jié)果告之1下,
當負載測試開始, Webservicer 的 obj.Properties["CurrentConnections"].Value 值,1直在增加, 當增加到24時,也就是我們摹擬的24個用戶,
我們在本機再摹擬編寫1段訪問WebServiceB方法的代碼,注意B方法是不會使線程休眠的,直接Return "ok" . 結(jié)果1直等到超時,也沒有OK顯示,
但此時 obj.Properties["CurrentConnections"].Value的值為25,
如果我們把負載并發(fā)數(shù)的設(shè)為23呢,再啟動負載測試, 當連接數(shù)到達23時, 再調(diào)用B方法,結(jié)果發(fā)現(xiàn),大約在3秒鐘,調(diào)用B方法成功了,顯示“OK”,
順便提1句:在ASP.NET 2.0中,引入了autoConfig屬性:
1 | < processModel autoConfig = "true" /> |
當值為true時,autoConfig在運行時修改配置以下:
如果大家說, 我有1臺猛機, 配置牛B, 這些默許的設(shè)置不夠,我需要自已動用配置, OK, 可以直接在machine.config里修改,
下面是示例代碼,
有些人會問: 這個東東1定要在 machine.config里修改嗎? 能不能在web.config里面呢,目前我在Webserive程序里面的Web.config
修改了 <add address = "*" maxconnection = "30" />, 但使用負載測試時,發(fā)現(xiàn)還是不行,只能到達24, 這就說明30并未起作用。
有知道的說明1下,為何不行? 依照微軟的說明,應(yīng)當在web.config里面是可以起作用的,
需要注意的是: 如果增加了maxconnection 數(shù)量, maxWorkerThreads的數(shù)量也需要增加相當?shù)臄?shù)量, 比如在雙核的CPU上, 修改maxconnection = 25,
則 maxWorkerThreads也需要修改成101, 由于 maxconnection = maxWorkerThreads --minFreeThreads
還有1個疑問: 如果IIS崩潰了, 這里正好有1個HTTP要求過來,那末IIS會怎樣處理? 答案中最好有微軟的官方說明。
結(jié)論: IIS的站點默許的并發(fā)連接數(shù)是12*CPU,也就是說默許設(shè)置下IIS在同1時刻能處理的最大要求數(shù)是12*CPU數(shù)量 。
歡迎猛烈拍磚,有甚么好建議你老就用力的提吧。謝謝你了。
上一篇 PHP讀取EXCEL的方法 下