I have code that references a web service, and I\'d like the address of that web service to be dynamic (read from a database, config file, etc.) so that it is easily changed
When you generate a web reference and click on the web reference in the Solution Explorer. In the properties pane you should see something like this:
Changing the value to dynamic will put an entry in your app.config.
Here is the CodePlex article that has more information.
Change URL behavior to "Dynamic".
As long as the web service methods and underlying exposed classes do not change, it's fairly trivial. With Visual Studio 2005 (and newer), adding a web reference creates an app.config (or web.config, for web apps) section that has this URL. All you have to do is edit the app.config file to reflect the desired URL.
In our project, our simple approach was to just have the app.config entries commented per environment type (development, testing, production). So we just uncomment the entry for the desired environment type. No special coding needed there.
Definitely using the Url property is the way to go. Whether to set it in the app.config, the database, or a third location sort of depends on your configuration needs. Sometimes you don't want the app to restart when you change the web service location. You might not have a load balancer scaling the backend. You might be hot-patching a web service bug. Your implementation might have security configuration issues as well. Whether it's production db usernames and passwords or even the ws security auth info. The proper separation of duties can get you into some more involved configuration setups.
If you add a wrapper class around the proxy generated classes, you can set the Url property in some unified fashion every time you create the wrapper class to call a web method.
open solition explorer
right click the webservice change URL Behavior to Dynamic
click the 'show all files' icon in solution explorer
in the web reference edit the Reference.cs file
change constructer
public Service1() {
this.Url = "URL"; // etc. string variable this.Url = ConfigClass.myURL
}
Just a note about difference beetween static and dynamic.