简体   繁体   English

在运行时设置服务URL

[英]Setting the service URL at runtime

When I am adding the "Web Reference" we are giving the address to the asmx page to visual studio. 当我添加“Web引用”时,我们将asmx页面的地址提供给visual studio。

How Can I set this at run time? 我怎样才能在运行时设置它?

I would have upvoted one of the other answers - they're almost correct. 我会赞成其中一个答案 - 他们几乎是正确的。

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

    // Now you're ready to call your service method 
    service.SomeUsefulMethod(); 
}

If a using block is not used, and an exception is thrown, then resources like network connections can be leaked. 如果未使用using块,并且抛出异常,则可能会泄漏网络连接等资源。

Just set the Url property of the object before you call any of the service methods: 只需在调用任何服务方法之前设置对象的Url属性:

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

// Now you're ready to call your service method
service.SomeUsefulMethod();
YourWebService service = new YourWebService();
service.Url = "http://www.example.com/YourWebService.asmx";
service.CallMethod();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM