如何更改WCF服务引用的地址? [英] How can I change the address of a WCF service reference?

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

问题描述

我有一个在内部使用的应用程序,并使用WCF来进行客户端和服务器部分之间的通信,但是它将很快需要部署到服务器名称不同的站点。 WCF服务使用netTcp绑定作为Windows服务托管。目前,使用Visual Studio中的添加服务参考命令来指定服务的地址。



是否可以使WCF服务的基址成为用户首选项,然后使服务引用在需要使用时动态构建URL。



所以例如,如果我有一个名为CustomerService的服务,是否可能对于不同地方的两个不同的用户来指定地址:

  net-tcp:// myserver1 / 

  tcp:// anotherserver / 

并将服务引用根据需要将其转换为

  net-tcp:// myserver1 / CustomerService 

  net-tcp:// anotherserver / CustomerService? 

谢谢,



Jim

解决方案

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

  var client = new MyServiceClient(
endpointConfigurationName,
net-tcp:// myserver1 /客户服务);

这样你可以覆盖你的app / web.config中存储的地址值



如果您直接使用 ChannelFactory< T> 类:

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


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.

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.

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/

and

net-tcp://anotherserver/

and have the service reference convert these as necessary into

net-tcp://myserver1/CustomerService

and

net-tcp://anotherserver/CustomerService?

Thanks,

Jim

解决方案

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");

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

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天全站免登陆