Dynamically switch WCF Web Service Reference URL path through config file
Asked Answered
M

6

55

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

Mistrust answered 18/2, 2011 at 0:11 Comment(0)
S
87

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

Sesame answered 18/2, 2011 at 0:16 Comment(3)
What is Server.IsLiveServer() :)?Statement
Server.IsLiveServer() is just a custom method (call it whatever you want) that does a check to determine which url to use.Intersidereal
Server.IsLiveServer() is just there to switch the URL as per the environment that consumes the APIThigmotaxis
P
26

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!

Prepare answered 14/7, 2011 at 9:24 Comment(0)
C
3

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.

Chondroma answered 18/2, 2011 at 0:19 Comment(0)
T
1

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.

Tenfold answered 11/3, 2022 at 16:47 Comment(0)
B
0

sure you can do this, have a look here: How to config clients for a wcf service?

it is absolutely normal to point to localhost in development and to change the address (url) in production in the web.config

Backcross answered 18/2, 2011 at 0:15 Comment(3)
i saw references of the development URL in 7 files: reference.svcmap, .xsd, .wsdl, .disco, .svcinfo & web.config. is this configuring web.config all i need?Mistrust
ah do you mean that you would like to change all of those, kind of overriding the hard coded values touching the web.config ?Backcross
@Martin: I bet that's a namespace and not your development URL.Turnery
D
0

you can´t chance endpoint url after any calling.

E.G.

in that case, you will get answer from NEWURL:

MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress("NEWURL"); 
client.Hello(); //return is hello response from NEWURL

but if you will call any method before changing url, the url will be used from app.config, like next example:

MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress("NEWURL"); 
client.Hello(); //return is hello response from BASEURL
Dearborn answered 27/8, 2018 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.