Change the url of a web service by reading from the web.config [duplicate]

百般思念 提交于 2019-12-12 09:14:29

问题


I have a WCF app that consumes an asmx web service. I use the web service in a million places in the app:

    public string LogOnUser(string username, string password, string db)
    {
        var wsi = new ASMXServiceInterface();
        return wsi.LogIn();
      }

    public string GetNotes(string username, string password, string db)
    {
        var wsi = new ASMXServiceInterface();
        return wsi.GetNotes();
      }

    etc, etc etc...

Of course i want to set the url of the service in the contructor, but its auto generated in reference.cs and if i change it there, it works but if i update my reference ( and i will) its lost and i have to manually do it again:

      /// <remarks/>
    public ASMXServiceInterface()
    {
        this.Url = 
    System.Web.Configuration.WebConfigurationManager.AppSettings["RQTCSServiceURL"];
   }

The web service url will need to be dynamic because different versions of it have been implemented. How can i set the url of my web service once in my WCF project so the url of the service can be changed in the web.config without having to do it in reference.cs?


回答1:


You can do so by adding a key to your web.config like:

<add key="webserviceURL" value ="https://mywebservice.com/WebService.asmx" />

Then in your code, do something like:

private static WebService createWebService() {
        WebService service= new WebService();

        string url = ConfigurationManager.AppSettings["webserviceURL"];
        if ( !string.IsNullOrEmpty(url) ) {
            service.Url = url;
        }

        return service;
    }



回答2:


You can change the autogeneated url for webservice by converting the UrlBehaviour to dynamic

Please see example on how to do http://www.codeproject.com/Articles/12317/How-to-make-your-Web-Reference-proxy-URL-dynamic



来源:https://stackoverflow.com/questions/11513609/change-the-url-of-a-web-service-by-reading-from-the-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!