How to configure an ASMX web service URL from a remote source

We are working on a legacy C # enterprise application. His client uses several web services whose URLs, among many other parameters, are read from the local app.config file. We want to transfer these parameters to the global database in order to simplify their management. However, I can’t understand how (and whether) it is possible to migrate web service URLs. They are read from the client service code generated by VS, and I cannot find a way to tell VS to use a different parameter provider than the one generated in Settings.Designer.cs .

We can overwrite the Url property of the service facade with the value that we want after creating it - this is the solution that is currently used in several places in the code. However, I would not like to touch on every part of our code base where any of these services is used (now and in the future). Even less would like to change the generated code.

Should there be a better, cleaner, safer solution - or is there?

Btw our application runs on .NET 2.0, and in the foreseeable future we will not move to newer versions of the platform.

+1
source share
1 answer

The Refernce.cs file created by Visual Studio indicates that the web service URL will be obtained from the settings:

 this.Url = global::ConsoleApplication1.Properties. Settings.Default.ConsoleApplication1_net_webservicex_www_BarCode; 

I believe that John Saunders gave you a wonderful suggestion in his comment. You need a SettingsProvider class that:

... defines the mechanism for storing configuration data used in application architecture .. The .NET Framework contains a single default settings provider, LocalFileSettingsProvider, which stores configuration data on the local file system. However, you can create alternative storage engines by getting the abstract sets the Provider class. A provider that uses a wrapper class is defined by decorating the wrapper class with SettingsProviderAttribute. If this attribute is not specified, the default is LocalFileSettingsProvider.

I don’t know how much you have progressed after this approach, but it should go pretty well:

  • Create the SettingsProvider class:

     namespace MySettings.Providers { Dictionary<string, object> _mySettings; class MySettingsProvider : SettingsProvider { // Implement the constructor, override Name, Initialize, // ApplicationName, SetPropertyValues and GetPropertyValues (see step 3 below) // // In the constructor, you probably might want to initialize the _mySettings // dictionary and load the custom configuration into it. // Probably you don't want make calls to the database each time // you want to read a setting value } } 
  • Extend the class definition for the partial project class YourProjectName.Properties.Settings and decorate it with SettingsProviderAttribute :

     [System.Configuration.SettingsProvider(typeof(MySettings.Providers.MySettingsProvider))] internal sealed partial class Settings { // } 
  • In the overridden GetPropertyValues method, you should get the display value from the _mySettings dictionary:

     public override SettingsPropertyValueCollection GetPropertyValues( SettingsContext context, SettingsPropertyCollection collection) { var spvc = new SettingsPropertyValueCollection(); foreach (SettingsProperty item in collection) { var sp = new SettingsProperty(item); var spv = new SettingsPropertyValue(item); spv.SerializedValue = _mySettings[item.Name]; spv.PropertyValue = _mySettings[item.Name]; spvc.Add(spv); } return spvc; } 

As you can see in the code, for this you need to know the name of the parameter that was added to app.config and Settings.settings when you added the link to the web service ( ConsoleApplication1_net_webservicex_www_BarCode ):

 <applicationSettings> <ConsoleApplication30.Properties.Settings> <setting name="ConsoleApplication1_net_webservicex_www_BarCode" serializeAs="String"> <value>http://www.webservicex.net/genericbarcode.asmx</value> </setting> </ConsoleApplication30.Properties.Settings> </applicationSettings> 

This is a very simple example, but you can use a more complex object to store configuration information in combination with other properties available in the context, such as item.Attributes or context , to get the correct configuration value.

+1
source

All Articles