日期:2014-05-18  浏览次数:20588 次

.net中的WebService如何动态配置引用地址?
.net中的WebService如何动态配置引用地址?

现在WebService的引用地址不确定,如果地址发生更改,需要重新编译程序。
我想把引用地址写在配置文件里面,实现动态引用。
这个怎么实现?请高手指教

------解决方案--------------------
动态调用WebService 
http://www.cnblogs.com/VisualStudio/archive/2008/10/29/1322228.html
------解决方案--------------------
探讨
.net中的WebService如何动态配置引用地址?

现在WebService的引用地址不确定,如果地址发生更改,需要重新编译程序。
我想把引用地址写在配置文件里面,实现动态引用。
这个怎么实现?请高手指教

------解决方案--------------------
NET 下动态设置 WebService 引用 
http://blog.csdn.net/hrbwgs1111/article/details/4522496
------解决方案--------------------
地址果断写config里面。用的时候直接webservies.Url="地址" 就哦了
------解决方案--------------------
为何引用的地址会不确定呢?这个本身就是应该要确定的。
------解决方案--------------------
探讨
地址果断写config里面。用的时候直接webservies.Url="地址" 就哦了

------解决方案--------------------
如果是本地通过引用服务的,可以直接修改地址属性的。

另外,直接用 HttpRequest 调用,地址也可以随时修改。
------解决方案--------------------
地址写相对路径,不要写绝对路径,不然可能会出现问题
------解决方案--------------------
关于动态添加WebService引用的地址的问题,我分析了一下,解决方案如下:
1.在应用程序中添加配置文件(如Winform的一般是app.config,webform的一般是web.config),在<appSettings>目录下添加一个配置WebService引用地址的节点,如:<add key="webServiceAddr" value="http://192.168.1.105:800/TestWebService.asmx?wsdl"/>
2.项目添加Web服务引用,如引用名为ServiceCenter,引用成功后,在打开目录Web References》ServiceCenter》Reference.map》Reference.cs的Reference.cs文件,这是一个WebService代理类。
不同的WebService生成的代理类不同。构造函数如:
public TestWebService() {
this.Url = global::WebServiceApp.Properties.Settings.Default.WebServiceApp_ServiceCenter_TestWebService;
if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
this.UseDefaultCredentials = true;
this.useDefaultCredentialsSetExplicitly = false;
}
else {
this.useDefaultCredentialsSetExplicitly = true;
}
}
重新添加一个构造函数,带有WebService引用地址的参数:
public TestWebService(string url)
{
if (!string.IsNullOrEmpty(url))
{
this.Url = url;
}
else
{
this.Url = global::WebServiceApp.Properties.Settings.Default.WebServiceApp_ServiceCenter_TestWebService; 
}
if ((this.IsLocalFileSystemWebService(this.Url) == true))
{
this.UseDefaultCredentials = true;
this.useDefaultCredentialsSetExplicitly = false;
}
else
{
this.useDefaultCredentialsSetExplicitly = true;
}
}
3.在应用程序中应用
private void button1_Click(object sender, EventArgs e)
{
string result = string.Empty;
string serviceAddr = string.Empty;
try
{
serviceAddr = System.Configuration.ConfigurationManager.AppSettings["webServiceAddr"].ToString();
//此处调用的是我们自己定义的构造函数,参数为WebService引用的地址
ServiceCenter.TestWebService webService = new WebServiceApp.ServiceCenter.TestWebService(serviceAddr);
result = webService.Test();
}
catch (Exception ex)
{
result = ex.Message;
}
MessageBox.Show(serviceAddr + "++++" + result);
}
4.修改WebService引用地址:
在Winform应用程序中,app.config等应用程序配置文件在生成的时候自动生成到了bin目录下面的应用程序名.exe.config文件,修改里面的webServiceAddr节点即可。
需要注意的一点就是,如果生成的时候把app.config文件也生成到了bin目录下,此时修改app.config里面的配置是无效,还必须得修改(应用程序名.exe.config)这个文件。如果是把webservice引用地址放在自定义的的xml文件中,则生成到bin目录下,响应bin目录下的xml文件即可。