Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically switch WCF Web Service Reference URL path through config file

Tags:

c#

asp.net

wcf

How do you dynamically switch WCF Web Service Reference URL path through config file ?

like image 315
Martin Ongtangco Avatar asked Feb 18 '11 00:02

Martin Ongtangco


4 Answers

Are you just wanting to override the URL that is in the config to a different url. Say you have a test service and a live service. You can just do this.

client.Endpoint.Address = new EndpointAddress(Server.IsLiveServer() ?
    @"LiveUrl" : @"TestURl"); 

Where those url come from wherever you want

like image 68
Erin Avatar answered Oct 17 '22 18:10

Erin


Just to expand on the answer from Erin: -

MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress(new Uri("insert new url here"),
    client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
client.Open();

HTH!

like image 30
Phil Lambert Avatar answered Oct 17 '22 18:10

Phil Lambert


There is no dynamic switching. Each time you want to use another URL you must create new instance of service proxy (client) and pass EndpointAddress or enpoint configuration name to the constructor.

like image 3
Ladislav Mrnka Avatar answered Oct 17 '22 18:10

Ladislav Mrnka


I have been trying to do the same thing but most of the accepted answers in various posts just change the address. Currently under .net 4.7, simply changing the address itself does not work. If you have two different servers and wanting it to switch from one to the other, you have to do this:

var client = new MyService.Service1Client();
var newAdrEndpoint = new EndpointAddress(new Uri("second server address"));
client = new MyService.Service1Client(client.Endpoint.Binding, newAdrEndpoint);

Essentially you need to create a new service using the same binding from the first server and passing in the new address. This is the simplest method I have found.

like image 1
wirble Avatar answered Oct 17 '22 17:10

wirble