How can I change the address of a WCF service reference?

旧街凉风 提交于 2020-01-03 17:12:31

问题


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


回答1:


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



回答2:


Isn't all of this information in the configuration in the app.config or web.config? Simply change the URL in the endpoint configuration.



来源:https://stackoverflow.com/questions/647993/how-can-i-change-the-address-of-a-wcf-service-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!