在运行时设置服务URL

Boh*_*ohn 3 c# app-config asmx

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

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

Joh*_*ers 6

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

using (YourService service = new YourService())
{
    service.Url = "http://some.other.url/"; 

    // Now you're ready to call your service method 
    service.SomeUsefulMethod(); 
}
Run Code Online (Sandbox Code Playgroud)

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


Jus*_*ner 5

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

YourService service = new YourService();
service.Url = "http://some.other.url/";

// Now you're ready to call your service method
service.SomeUsefulMethod();
Run Code Online (Sandbox Code Playgroud)