We are working on a legacy C# enterprise app. Its client uses several web services, whose URLs, among lots of other settings, are read from the local app.config file. We want to migrate these settings into a global DB to simplify their management. However, I can’t figure out how (and whether) it is possible to migrate the web service URLs. These are read from the service client code generated by VS and I can’t seem to find a way to tell VS to use a different settings provider than the one generated into Settings.Designer.cs .
We can overwrite the service facade’s Url property with the value we want, after it is created – this is the solution currently used in several places in the code. However, I wouldn’t like to touch every part of our codebase where any of these services is used (now and in the future). Even less would I like to modify generated code.
There has to be a better, cleaner, safer solution – or is there?
Btw our app runs on .NET 2.0 and we won’t migrate to newer versions of the platform in the foreseeable future.
The
Refernce.csfile that is generated by the Visual Studio indicates that the URL of the webservice will be retrieved from the settings:I believe that John Saunders gave you a wonderful suggestion in his comment. You need a
SettingsProviderclass which:I don’t know how much you have progressed following this approach, but it should go pretty straighforward:
Create the
SettingsProviderclass:Extend the class definition for the project’s
YourProjectName.Properties.Settingspartial class and decorate it with theSettingsProviderAttribute:In the overridden
GetPropertyValuesmethod, you have to get the mapped value from the_mySettingsdictionary:As you can see in the code, in order to do that, you need to know the setting name as it was added in the
app.configand theSettings.settingswhen you have added the reference to the web service (ConsoleApplication1_net_webservicex_www_BarCode):This is a very simple example, but you might use a more complex object to store the configuration information in conjunction with other properties available in the context such as
item.Attributesorcontextin order to get the proper configuration value.