Setting the service URL at runtime

后端 未结 3 1840
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 22:17

When I am adding the \"Web Reference\" we are giving the address to the asmx page to visual studio.

How Can I set this at run time?

相关标签:
3条回答
  • 2020-12-06 22:36

    Just set the Url property of the object before you call any of the service methods:

    YourService service = new YourService();
    service.Url = "http://some.other.url/";
    
    // Now you're ready to call your service method
    service.SomeUsefulMethod();
    
    0 讨论(0)
  • 2020-12-06 22:36
    YourWebService service = new YourWebService();
    service.Url = "http://www.example.com/YourWebService.asmx";
    service.CallMethod();
    
    0 讨论(0)
  • 2020-12-06 22:59

    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.

    0 讨论(0)
提交回复
热议问题