c#动态改变webservice的url访问地址
1、添加一个App.config配置文件。
2、配置服务http://Lenovo-PC:80/EvisaWS/WharfService?wsdl,那么在上面的文件中就会自动生成服务的配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WharfWSBeanBinding" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://Lenovo-PC:80/EvisaWS/WharfService"
binding="basicHttpBinding" bindingConfiguration="WharfWSBeanBinding"
contract="WharfService.WharfWSBean" name="WharfService" />
</client>
</system.serviceModel>
</configuration>
3、动态添加新的地址,后面的?wsdl没有也可以:
WharfWSBeanClient c = new WharfWSBeanClient("WharfService", "新的地址例如:http://192.168.1.194/EvisaWS/WharfService?wsdl");
4、新服务器的地址可以存放到注册表或者环境变量里面,下面是新存放到注册表,然后再存放到环境变量里,访问的时候先从环境变量里面获取:
4.1 存到注册表:
RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\Client");
key.SetValue("ip", "192.168.1.1");
4.2 从注册表获取值:
String ip =Environment.GetEnvironmentVariable("myconfigip");
if (ip == null || ip.Equals(""))
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Client");
ip = key.GetValue("myip") as string;
Environment.SetEnvironmentVariable("myconfigip", ip);
}
上面是先从环境变量里面取,如果没有则从注册表取,之后又存放到环境变量里,以后在进程内访问时直接从环境变量里面取就可以了。