WCF并發能力提高技巧分享
WCF服務中的并發能力可以通過一些手段進行提高,來幫助開發人員進行更好的使用。在這里我們將會針對各種提高辦法做一個詳細介紹,以方便大家對此的理解,希望你能從中獲得一些幫助。#t#
WCF并發能力提高步驟:
1.把同樣的WCF服務,在多個端口上"啟動"(即同時運行多個wcf的實例,但每個實例都監聽不同的端口)
2.用svcutil.exe生成的代理類,里面有N多構造函數的重載版本,觀察一下類似下面的這個版本
- public AstroServiceClient(string endpointConfigurationName) :
- base(endpointConfigurationName)
- {
- }
即傳入配置名生與代碼類的實例,我們在web.config中的wcf配置節,做如下處理:
- < 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>
即對應多個wcf服務端的實例,配置多個name的endpoint節點
3.修改客戶端的調用代碼提高WCF并發能力
把原來類似這樣的代碼:
- using (AstroServiceClient _client = new AstroServiceClient())
改成
- using (AstroServiceClient _client =
new AstroServiceClient(new Random().Next(1, 4).ToString()))
即客戶端隨機從多個wcf服務端的host中挑一個,生成代碼類實例
大功告成,說白了就是把一個wcf的host分身成了3個,并且客戶端隨機調用3者之一。以上就是WCF并發能力的相關提高方法介紹。