如何更改WCF服务引用的地址?

编程入门 行业动态 更新时间:2024-10-27 17:14:46
本文介绍了如何更改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服务引用的地址?

本文发布于:2023-11-07 09:09:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   地址   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!