WCF并發(fā)能力提高技巧分享
WCF服務(wù)中的并發(fā)能力可以通過(guò)一些手段進(jìn)行提高,來(lái)幫助開(kāi)發(fā)人員進(jìn)行更好的使用。在這里我們將會(huì)針對(duì)各種提高辦法做一個(gè)詳細(xì)介紹,以方便大家對(duì)此的理解,希望你能從中獲得一些幫助。#t#
WCF并發(fā)能力提高步驟:
1.把同樣的WCF服務(wù),在多個(gè)端口上"啟動(dòng)"(即同時(shí)運(yùn)行多個(gè)wcf的實(shí)例,但每個(gè)實(shí)例都監(jiān)聽(tīng)不同的端口)
2.用svcutil.exe生成的代理類(lèi),里面有N多構(gòu)造函數(shù)的重載版本,觀察一下類(lèi)似下面的這個(gè)版本
- public AstroServiceClient(string endpointConfigurationName) :
- base(endpointConfigurationName)
- {
- }
即傳入配置名生與代碼類(lèi)的實(shí)例,我們?cè)趙eb.config中的wcf配置節(jié),做如下處理:
- < client>
- < endpoint address="http://localhost:8001/Astro/"
binding="wsHttpBinding"- bindingConfiguration="WSHttpBinding_IAstroService"
contract="IAstroService"- name="1">
- < identity>
- < dns value="localhost" />
- < /identity>
- < /endpoint>
- < endpoint address="http://localhost:8002/Astro/"
binding="wsHttpBinding"- bindingConfiguration="WSHttpBinding_IAstroService"
contract="IAstroService"- name="2">
- < identity>
- < dns value="localhost" />
- < /identity>
- < /endpoint>
- < endpoint address="http://localhost:8003/Astro/"
binding="wsHttpBinding"- bindingConfiguration="WSHttpBinding_IAstroService"
contract="IAstroService"- name="3">
- < identity>
- < dns value="localhost" />
- < /identity>
- < /endpoint>
- < /client>
即對(duì)應(yīng)多個(gè)wcf服務(wù)端的實(shí)例,配置多個(gè)name的endpoint節(jié)點(diǎn)
3.修改客戶(hù)端的調(diào)用代碼提高WCF并發(fā)能力
把原來(lái)類(lèi)似這樣的代碼:
- using (AstroServiceClient _client = new AstroServiceClient())
改成
- using (AstroServiceClient _client =
new AstroServiceClient(new Random().Next(1, 4).ToString()))
即客戶(hù)端隨機(jī)從多個(gè)wcf服務(wù)端的host中挑一個(gè),生成代碼類(lèi)實(shí)例
大功告成,說(shuō)白了就是把一個(gè)wcf的host分身成了3個(gè),并且客戶(hù)端隨機(jī)調(diào)用3者之一。以上就是WCF并發(fā)能力的相關(guān)提高方法介紹。