通过配置文件动态切换WCF Web服务引用URL路径

时间:2011-02-18 00:11:07

标签: c# asp.net wcf

如何通过配置文件动态切换WCF Web服务引用URL路径?

5 个答案:

答案 0 :(得分:79)

您是否只想将配置中的URL覆盖到其他网址。假设您有测试服务和实时服务。你可以这样做。

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

这些网址来自您想要的任何地方

答案 1 :(得分:22)

只是为了扩展艾琳的答案: -

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!

答案 2 :(得分:3)

没有动态切换。每次要使用其他URL时,都必须创建服务代理(客户端)的新实例,并将EndpointAddress或enpoint配置名称传递给构造函数。

答案 3 :(得分:0)

确定你可以这样做,看看这里:How to config clients for a wcf service?

在开发中指向localhost并在web.config中更改生产中的地址(url)是绝对正常的

答案 4 :(得分:0)

任何呼叫后您都无法访问端点URL。

E.G。

在这种情况下,您将从NEWURL得到答案:

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

但是,如果您将在更改url之前调用任何方法,则该URL将在app.config中使用,如以下示例所示:

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