简体   繁体   English

如何在C#中选择服务参考的IP?

[英]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? 我有一个简单的控制台应用程序和一个WCF服务,它们已经连接并且可以正常工作,但是现在我想知道如何使我的应用程序连接到这个相同的Web服务,但是要在其他地方托管? The idea is to have several instances of the application that connects to the web service, but they might be connected in other servers. 这个想法是让应用程序的多个实例连接到Web服务,但是它们可能连接在其他服务器上。 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 ): 您可以将服务客户端的Endpoint.Address设置为新地址(从http://bytes.com/topic/net/answers/696769-change-wcf-client-endpoint-address盗取的代码):

HelloWorld.HelloWorldClient client = new WCFHelloWorldClient.HelloWorld.HelloWorldClient(); HelloWorld.HelloWorldClient客户=新的WCFHelloWorldClient.HelloWorld.HelloWorldClient();

//change the endpointAddress here client.Endpoint.Address = new EndpointAddress(" http://localhost:8899/BasicHost/HelloWorld "); //在此处更改endpointAddress 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? 在这里也可以找到类似的问题(带有答案): 如何更改WCF服务引用的地址? .

Read up on WCF Client configuration. 阅读WCF客户端配置。

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 一切都可以在.config文件中完成,您可以将其发布,和/或以编程方式(如果您需要使其更加动态)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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