這是在客戶端的東西??
如果是這樣,你需要創建WsHttpBinding的實例,和的EndpointAddress,然後通過這兩個給代理客戶端構造函數,這兩個作爲參數。
// using System.ServiceModel;
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost:9000/MyService"));
MyServiceClient client = new MyServiceClient(binding, endpoint);
如果它是對事物的服務器端,你需要以編程方式創建自己的ServiceHost實例,以及相應的服務端點添加到它。
ServiceHost svcHost = new ServiceHost(typeof(MyService), null);
svcHost.AddServiceEndpoint(typeof(IMyService),
new WSHttpBinding(),
"http://localhost:9000/MyService");
當然你可以添加到您的服務主機的服務端點的倍數。完成後,您需要通過調用.Open()方法來打開服務主機。
如果您希望能夠在運行時動態選擇要使用的配置,可以定義多個配置,每個配置都有唯一的名稱,然後調用相應的構造函數(用於服務主機或代理客戶機)以及您希望使用的配置名稱。
E.g.你可以輕鬆擁有:
<endpoint address="http://mydomain/MyService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IASRService"
contract="ASRService.IASRService"
name="WSHttpBinding_IASRService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="https://mydomain/MyService2.svc"
binding="wsHttpBinding" bindingConfiguration="SecureHttpBinding_IASRService"
contract="ASRService.IASRService"
name="SecureWSHttpBinding_IASRService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="net.tcp://mydomain/MyService3.svc"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IASRService"
contract="ASRService.IASRService"
name="NetTcpBinding_IASRService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
(三個不同的名字,不同的參數,通過指定不同的bindingConfigurations),然後只選擇一個正確的實例化你的服務器(或客戶代理)。
但在這兩種情況下 - 服務器和客戶端 - 你實際上是創建服務主機或代理客戶端之前來接。 一旦創建,這些都是不可改變 - 你不能調整它們一旦他們啓動和運行。
馬克
鍊金,你可能想,如果你發現自己需要在運行時修改一個app.config重新評估你的答案接受 – 2012-02-23 19:54:40
的選擇,根據我的經驗,這可能意味着你缺少一個.NET提供的手段完成你正在嘗試做的事情。亞歷克斯諾特的答案在這種情況下指出它,如果你真的想要做的是擊中一個不同的地址,在同一個確切的服務託管。 – 2014-04-09 17:18:52