views:

32

answers:

1

We inherited some code that makes use of a web service on an external server from a third party. Currently all the references are done in the project directly and thus compiled to that location and such. This issue we have is that there is a test and production server for the web service with different URLs.

Is there a simple means to make the web reference more dynamic so that it can be defined in a web.config and not require changing the actual source and recompiling to switch between servers?

+4  A: 

You could put the url in the appSettings section of the web.config file:

<appSettings>
    <add key="wsUrl" value="http://example.com/staging.asmx" />
</appSettings>

And then read the value and in the constructor of the webservice proxy class and assign it to the Url property:

public SomeProxy()
{
    Url = ConfigurationManager.AppSettings["wsUrl"];
}
Darin Dimitrov
Also, if you are using a web deployment project, you can have the installer copy the correct web.config file (which would contain different values for wsUrl depending on the target environment) based on your target environment (dev, stage, production).
David Lively
Or put it in machine.config, that way no need to touch web.config.
Darin Dimitrov
Just FYI in .NET 2.0 there are sections for this specifically and so you wouldnt need to use custom getters (from ConfigManager) in code.
GrayWizardx