摘要:[WCF] 在程式中置換 EndPoint Address
有時候在 Winform Client 程式須動態切換聯繫的服務端點,例如要從 www.origin.com/site/Service.svc 轉至 www.new.com/website/Service.svc,Client 的 app.config 如下
程式中重新設定 EndpointAddress,如下
string uri = "http://www.new.com/website/Service.svc"; string dns = "www.new.com"; ServiceClient service = new ServiceClient(); AddressHeaderCollection headers = service.Endpoint.Address.Headers; EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity(dns); EndpointAddress addr = new EndpointAddress(new Uri(uri), identity, headers); service.Endpoint.Address = addr;