系统架构需要使用Web service来降低耦合性。但是,现场布置的时候,WebService的地址是不固定的。
可以使用修改exe文件的对应的exe.config中的设置来达到目的。
exe.config文件是一个XML配置文件,其中描述了Web Service的地址。节点@"configuration/system.serviceModel/client/endpoint" 中的"address"属性的值就是Web Service的地址。例如, “http://192.168.0.100/helloWorld/Service.asmx”。
创建一个类DynamicURL来进行Web Service地址修改操作。

class DynamicURL
为了测试,建议Web Service, HelloWorld. 里面有方法HelloWorld,返回字符串“Hello World”。 拷贝HelloWorld成HelloWorld2,然后将HelloWorld的返回值设置成“This is the second Hello world Web Service.” 以便调用的时候,可以区别两个Web Service。
在新建的窗体上添加2个按钮和2个TextBox控件。
往工程中添加服务引用 ServiceReference1,VS2008会自动添加 “http://192.168.0.100/helloWorld/Service.asmx”上服务描述。
Form1为测试窗体。

测试窗体
测试步骤:
在IIS中为两个Web Services分别设置虚拟目录HelloWorld和HelloWorld2.
运行程序,点击调用按钮查看调用结果,再将URL地址从http://192.168.0.100/helloWorld/Service.asmx”修改成http://192.168.0.100/helloWorld2/Service.asmx”。点击“修改Web Service地址”,再调用Web Service查看结果。
两次Web Service调用结果分别为 “Hello World”和“This is the second Hello world Web Service.” 。
可以看到修改Web Service地址的效果了。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-659716/ ,如需转载,请注明出处,否则将追究法律责任。