如何更改WCF服务端点?

时间:2010-02-10 05:35:16

标签: wcf

我有三个以上的网络服务, 其中一个是主站点,其他是客户站点。

在我的用户界面中,一个文本框可用,在该文本框中我需要提供目标端点地址 从该文本框值我需要调用客户服务。

例如:

Client1端点服务名称:

                 http://localhost:1524/WebServiceService.svc" 

Client2端点服务名称:

通过

Rajagopalk

                 http://localhost:8085/WebServiceService.svc" 

如果我在文本框中提供“localhost:1524”,则Client1服务将调用, 如果我在文本框中提供“localhost:8085”,则Client2服务将调用,

1 个答案:

答案 0 :(得分:6)

您是否在IIS中托管WCF服务?在这种情况下,您的服务地址由IIS配置和服务的* .svc文件所在的虚拟目录确定。

因此,要更改服务器上的内容,您需要检查并修改IIS配置。

要在客户端进行更改,可以使用web.config(对于ASP.NET Web)或(applicationName).exe.config,其中应包含端点定义 - 在那里更改端点地址:

<client> 
   <endpoint name="YourEndpointName"
       address="http://localhost:8085/WebServiceService.svc" 
       binding="......." bindingConfiguration="............."
       contract="..................." />
</client>

您需要在address=配置元素的<endpoint>属性中指定完整的目标Web服务地址。

您可以为同一服务定义多个端点,并选择在实例化客户端代理时使用哪个端点:

MyServiceProxy client = new MyServiceProxy("name of endpoint configuration");

通过这种方式,您可以轻松地在多个端点定义之间切换。

更新:如果要以编程方式从代码设置客户端地址,则需要在创建客户端代理时执行以下操作:

// create custom endpoint address in code - based on input in the textbox
EndpointAddress epa = new EndpointAddress(new Uri(textbox.Text));

// instantiate your cilent proxy using that custom endpoint address 
// instead of what is defined in the config file
MyServiceProxy client = new MyServiceProxy("name of endpoint configuration", epa);