我怎样才能改变一个WCF服务引用地址? [英] How can I change the address of a WCF service reference?

查看:303
本文介绍了我怎样才能改变一个WCF服务引用地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在内部使用,并使用WCF客户端和服务器部分之间通信的应用,但它很快就需要部署到网站,服务器名称不同。该WCF服务的托管与使用netTcp绑定Windows服务。目前,在Visual Studio中使用的添加服务引用命令中指定的服务的地址。

I have an application which is used internally and uses WCF for communication between client and server portions, however it will soon need deploying to sites where server names are different. The WCF services are hosted as a Windows service using the netTcp binding. At the moment, the addresses of the services are specified using the Add Service Reference command in Visual Studio.

是否有可能使WCF的基址服务的用户preference,然后进行服务引用动态构造URL时,它需要使用。

Is it possible to make the base address of the WCF services a user preference, and then make the service reference dynamically construct the URL when it needs to use.

因此​​,举例来说,如果我有一个服务名为的CustomerService,有可能是两个不同的用户在不同的地方指定地址:

So for example, if I had a service named "CustomerService", is it possible for two separate users in different places to specify the addresses:

net-tcp://myserver1/

net-tcp://anotherserver/

和有服务引用这些必要的转换成

and have the service reference convert these as necessary into

net-tcp://myserver1/CustomerService

net-tcp://anotherserver/CustomerService?

谢谢

吉姆

推荐答案

在实例化客户端代理类(一个派生自ClientBase并实现您的服务合同),您可以指定一个远程地址:

When you instantiate the client proxy class (the one that derives from ClientBase and implements your service contract) you can specify a remote address:

var client = new MyServiceClient(
    "endpointConfigurationName", 
    "net-tcp://myserver1/CustomerService");

此方式,您可以覆盖存储在您的应用程序/ web.config中的地址值

This way you can override the address value stored in your app/web.config

另一个选项是直接在的ChannelFactory< T> 类:

Another option if you use directly the ChannelFactory<T> class:

var factory = new ChannelFactory<IMyServiceContract>(
    "endpointConfigurationName", 
    new EndpointAddress("net-tcp://myserver1/CustomerService"));
IMyServiceContract proxy = factory.CreateChannel();

这篇关于我怎样才能改变一个WCF服务引用地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆