是否可以使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/CustomerServiceand
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服务引用的地址?
发布评论