Asked  7 Months ago    Answers:  5   Viewed   318 times

I have the following problem:
We have an application that loads modules (add ons). These modules might need entries in the app.config (e.g. WCF configuration). Because the modules are loaded dynamically, I don't want to have these entries in the app.config file of my application.
What I would like to do is the following:

  • Create a new app.config in memory that incorporates the config sections from the modules
  • Tell my application to use that new app.config

Note: I do not want to overwrite the default app.config!

It should work transparently, so that for example ConfigurationManager.AppSettings uses that new file.

During my evaluation of this problem, I came up with the same solution as is provided here: Reload app.config with nunit.
Unfortunately, it doesn't seem to do anything, because I still get the data from the normal app.config.

I used this code to test it:

Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);
Console.WriteLine(Settings.Default.Setting);

var combinedConfig = string.Format(CONFIG2, CONFIG);
var tempFileName = Path.GetTempFileName();
using (var writer = new StreamWriter(tempFileName))
{
    writer.Write(combinedConfig);
}

using(AppConfig.Change(tempFileName))
{
    Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);
    Console.WriteLine(Settings.Default.Setting);
}

It prints the same values twices, although combinedConfig contains other values than the normal app.config.

 Answers

100

The hack in the linked question works if it is used before the configuration system is used the first time. After that, it doesn't work any more.
The reason:
There exists a class ClientConfigPaths that caches the paths. So, even after changing the path with SetData, it is not re-read, because there already exist cached values. The solution is to remove these, too:

using System;
using System.Configuration;
using System.Linq;
using System.Reflection;

public abstract class AppConfig : IDisposable
{
    public static AppConfig Change(string path)
    {
        return new ChangeAppConfig(path);
    }

    public abstract void Dispose();

    private class ChangeAppConfig : AppConfig
    {
        private readonly string oldConfig =
            AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();

        private bool disposedValue;

        public ChangeAppConfig(string path)
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
            ResetConfigMechanism();
        }

        public override void Dispose()
        {
            if (!disposedValue)
            {
                AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfig);
                ResetConfigMechanism();


                disposedValue = true;
            }
            GC.SuppressFinalize(this);
        }

        private static void ResetConfigMechanism()
        {
            typeof(ConfigurationManager)
                .GetField("s_initState", BindingFlags.NonPublic | 
                                         BindingFlags.Static)
                .SetValue(null, 0);

            typeof(ConfigurationManager)
                .GetField("s_configSystem", BindingFlags.NonPublic | 
                                            BindingFlags.Static)
                .SetValue(null, null);

            typeof(ConfigurationManager)
                .Assembly.GetTypes()
                .Where(x => x.FullName == 
                            "System.Configuration.ClientConfigPaths")
                .First()
                .GetField("s_current", BindingFlags.NonPublic | 
                                       BindingFlags.Static)
                .SetValue(null, null);
        }
    }
}

Usage is like this:

// the default app.config is used.
using(AppConfig.Change(tempFileName))
{
    // the app.config in tempFileName is used
}
// the default app.config is used.

If you want to change the used app.config for the whole runtime of your application, simply put AppConfig.Change(tempFileName) without the using somewhere at the start of your application.

Tuesday, June 1, 2021
 
DMTintner
answered 7 Months ago
27

You can either use the open-source HotSpot VM or the commercial JRebel IDE plugin to easily achieve your goal (view comparison table here).

Wednesday, June 9, 2021
 
hakimoun
answered 6 Months ago
28

Use this:

var NewLanguage = (string)((ComboBoxItem)e.AddedItems[0]).Tag;

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = NewLanguage;

Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
//Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();

Windows.ApplicationModel.Resources.Core.ResourceManager.Current.DefaultContext.Reset();

and then reload your Page, using Navigate method:

if (Frame != null)
    Frame.Navigate(typeof(MyPage));
Thursday, August 5, 2021
 
Andras Zoltan
answered 4 Months ago
28

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.PolymorphicConfigurationElementCollection<T> from EntLib5 do this job as a charm.

Saturday, October 9, 2021
 
Sadik
answered 2 Months ago
74

Try putting the configSections as the first child element of configuration, because configSections should be the first element of configurations

So your config file will go like this:

<configuration>

  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Vegi_Manager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
    </sectionGroup>
  </configSections>

  <connectionStrings>
    <add name="ConStr" connectionString="Integrated Security=false;Persist Security Info=False;User ID=funny;password=veryfunny;Initial Catalog=vegimanager;Data Source=.sqlexpress;"/>
  </connectionStrings>

  <userSettings>
    <Vegi_Manager.Properties.Settings>
      <setting name="FIRMNAME" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMADDRESS" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMCITY" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMSTATE" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMPHONE" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMMOBILE" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMEMAIL" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMTIN" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMPAN" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMMANDITAXNO" serializeAs="String">
        <value/>
      </setting>
      <setting name="INITIALFONFIGDONE" serializeAs="String">
        <value>False</value>
      </setting>
      <setting name="FIRMJURISDICTION" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMBANKDETAILS" serializeAs="String">
        <value/>
      </setting>
      <setting name="FIRMDETAILS" serializeAs="String">
        <value/>
      </setting>
      <setting name="BILLFORMATNO" serializeAs="String">
        <value>0</value>
      </setting>
      <setting name="PRINTERNAME" serializeAs="String">
        <value/>
      </setting>
    </Vegi_Manager.Properties.Settings>
  </userSettings>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>
Sunday, October 31, 2021
 
okapi
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share