简体   繁体   中英

How to chose the IP of the service reference in C#?

I have a simple console application and a WCF service, which are already connected and functional, but now I want to know how to make my application connect to this same web service, but hosted somewhere else? The idea is to have several instances of the application that connects to the web service, but they might be connected in other servers. Is there a way to do this without having to compile the same software several times?

You can set the service client's Endpoint.Address to a new address (code stolen from http://bytes.com/topic/net/answers/696769-change-wcf-client-endpoint-address ):

HelloWorld.HelloWorldClient client = new WCFHelloWorldClient.HelloWorld.HelloWorldClient();

//change the endpointAddress here client.Endpoint.Address = new EndpointAddress(" http://localhost:8899/BasicHost/HelloWorld ");

You can also set address in the client's constructor, but that will make you to also enter the configuration name which I think is a bit messy.

A similar question (with answer) is also found here: How can I change the address of a WCF service reference? .

Read up on WCF Client configuration.

Everything can be done in .config files which you can just post around, and/or programmatically if you need it to be even more dynamic

http://msdn.microsoft.com/en-us/library/ms731745.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM