<p>有时候需要动态的设置 WebService 的址,这样发布到不同的服务器时就要重新生成,为此我们需要在web.config中动态配置WebService的地址,在网上查了很多资料,其中这种方法感觉很好用也很好实现,原文VB.NET实现。本人已改为C#版</p><p>首先手动的添加一个Web引用(这个就不用说了吧)</p><div style="page-break-after: always;"><span style="display: none;"><!--more-->& nbsp ;</span></div><p>然后修改本地的代理类(添加一个新类,继承你的 WebService代理类)</p><p>实例:</p><p>namespace Web_Service</p><p>{</p><p>[System.Diagnostics.DebuggerStepThrough(),System.ComponentModel.DesignerCategory("code"),</p><p>System.Web.Services.WebServiceBinding(Name = "", Namespace = "")]</p><p>public class DynWebService : SelfWebService</p><p>{</p><p>public DynWebService() : base()</p><p>{</p><p>//设置默认webService的地址</p><p>this.Url = "http://localhost/WebService.asmx";;</p><p>}</p><p>public DynWebService(string webUrl) : base()</p><p>{</p><p>this.Url = webUrl;</p><p>}</p><p>}</p><p>}</p><p>说明:SelfWebService 你引用的 WebService</p><p>Web Service的URI部署到配置文件里</p><p><add key="WebServiceKey"value="http://xxxx/WebService.asmx";/></p><p>最后实现</p><p>private void WebServiceTest()</p><p>{</p><p>string webServiceUrl = ConfigurationManager.AppSettings["WebServiceKey "].ToString();</p><p>Web_Service.DynWebService dws = new Web_Service.DynWebService(webServiceUrl);</p><p>string result = dws.HelloWorld();</p><p>}
OK 到这里就搞定了</p>