步驟:
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"
name="2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="http://localhost:8003/Astro/" binding="wsHttpBinding"
name="3">
</client>
即對應多個wcf服務端的執行個體,配置多個name的endpoint節點
3.修改用戶端的調用代碼
把原來類似這樣的代碼:
using (AstroServiceClient _client = new AstroServiceClient())
改成
using (AstroServiceClient _client = new AstroServiceClient(new Random().Next(1, 4).ToString()))
即用戶端随機從多個wcf服務端的host中挑一個,生成代碼類執行個體
大功告成,說白了就是把一個wcf的host分身成了3個,并且用戶端随機調用3者之一
作者:菩提樹下的楊過