在运行时设置服务URL

当我添加“Web引用”时,我们将asmx页面的地址提供给visual studio。

我怎样才能在运行时设置它?

只需在调用任何服务方法之前设置对象的Url属性:

YourService service = new YourService(); service.Url = "http://some.other.url/"; // Now you're ready to call your service method service.SomeUsefulMethod(); 

我会赞成其中一个答案 – 他们几乎是正确的。

 using (YourService service = new YourService()) { service.Url = "http://some.other.url/"; // Now you're ready to call your service method service.SomeUsefulMethod(); } 

如果未使用using块,并且抛出exception,则可能会泄漏网络连接等资源。

 YourWebService service = new YourWebService(); service.Url = "http://www.example.com/YourWebService.asmx"; service.CallMethod();