Can I change a web service reference URL in the Config file?

Nick picture Nick · Sep 9, 2009 · Viewed 10.2k times · Source

I have an application targeting the 2.0 .NET framework. The solution is using the VS web service reference folder. A grep through the solution reveals that this URL lives in a handful of files. However in the deployed application a search shows that the URL lives in only the .config. So what happened to the .disco and .wsdl? Are they compiled into the .exe? Basically, I need to update the URL and I need to know if this requires a new build.

Thanks!

Answer

p.campbell picture p.campbell · Sep 9, 2009

Yes, you can change the URL that's being referenced at runtime.

If it's in a .config file, IIS will your app should detect the change in the .config file and load the new value. If not, then you'd have to restart the client. Perhaps you can stop and start the Web Site in IIS.

Further, you can definitely WRITE your code to read from a .config file.

  var myWS = new MyWebService();
  myWS.Url = WebServiceURL;
  myWS.SomeMethod();                     

private static string WebServiceURL { 
   get { return ConfigurationManager.AppSettings["MyWebServiceURL"].ToString(); }           }

Meanwhile in your .config file, you have:

  <appSettings>
    <add key="MyWebServiceURL" value="http://blah/foo/bar.asmx" />
  </appSettings>