Dynamically switch WCF Web Service Reference URL path through config file

后端 未结 5 2031
误落风尘
误落风尘 2020-12-01 02:56

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

相关标签:
5条回答
  • 2020-12-01 03:10

    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

    0 讨论(0)
  • 2020-12-01 03:17

    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

    0 讨论(0)
  • 2020-12-01 03:20

    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
    
    0 讨论(0)
  • 2020-12-01 03:22

    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.

    0 讨论(0)
  • 2020-12-01 03: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!

    0 讨论(0)
提交回复
热议问题