Change default app.config at runtime

后端 未结 8 2119
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 05:50

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 t

8条回答
  •  猫巷女王i
    2020-11-22 06:36

    Wonderful discussion, I've adding more comments to ResetConfigMechanism method to understand the magic behind the statement/calls in the method. Also added file path exist check

    using System;//AppDomain
    using System.Linq;//Where
    using System.Configuration;//app.config
    using System.Reflection;//BindingFlags
    using System.Io;
    
    /// 
    /// Use your own App.Config file instead of the default.
    /// 
    /// 
    public static void ChangeAppConfig(string NewAppConfigFullPathName)
    {
        if(File.Exists(NewAppConfigFullPathName)
        {
          AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", 
          NewAppConfigFullPathName);
          ResetConfigMechanism();
          return;
        }
    }
    
    /// 
    /// Remove cached values from ClientConfigPaths.
    /// Call this after changing path to App.Config.
    /// 
    private static void ResetConfigMechanism()
    {
        BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Static;
          /* s_initState holds one of the four internal configuration state.
              0 - Not Started, 1 - Started, 2 - Usable, 3- Complete
    
             Setting to 0 indicates the configuration is not started, this will 
             hint the AppDomain to reaload the most recent config file set thru 
             .SetData call
             More [here][1]
    
          */
        typeof(ConfigurationManager)
            .GetField("s_initState", Flags)
            .SetValue(null, 0);
    
    
        /*s_configSystem holds the configuration section, this needs to be set 
            as null to enable reload*/
        typeof(ConfigurationManager)
            .GetField("s_configSystem", Flags)
            .SetValue(null, null);
    
          /*s_current holds the cached configuration file path, this needs to be 
             made null to fetch the latest file from the path provided 
            */
        typeof(ConfigurationManager)
            .Assembly.GetTypes()
            .Where(x => x.FullName == "System.Configuration.ClientConfigPaths")
            .First()
            .GetField("s_current", Flags)
            .SetValue(null, null);
        return;
    }
    

提交回复
热议问题