【问题标题】:Setting the service URL at runtime在运行时设置服务 URL
【发布时间】:2015-05-12 01:52:29
【问题描述】:

当我添加“Web 参考”时,我们会将 asmx 页面的地址提供给 Visual Studio。

如何在运行时进行设置?

【问题讨论】:

    标签: c# asmx app-config


    【解决方案1】:

    我会赞成其他答案之一 - 他们几乎是正确的。

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

    如果未使用 using 块,并引发异常,则可能会泄漏网络连接等资源。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:
        YourWebService service = new YourWebService();
        service.Url = "http://www.example.com/YourWebService.asmx";
        service.CallMethod();
        

        【讨论】:

          猜你喜欢
          • 2012-05-16
          • 2021-03-02
          • 2021-11-26
          • 2015-08-07
          • 1970-01-01
          • 2021-09-18
          • 2023-04-08
          • 2011-06-04
          • 1970-01-01
          相关资源
          最近更新 更多