ComVisible.NET assembly and app.config

  • I have a .NET assembly with some classes marked as ComVisible
  • This assembly is registered in regasm /codebase "assembly_path"
  • I have the name app.config (actually - MyAssemblyName.dll.config) that are in the build folder
  • I access appSettings in my assembly through ConfigurationManager.AppSettings["SettingName"]
  • I have a VBScript file that creates my COM object through CreateObject("...")
  • When an object is created (from VBScript), ConfigurationManager.AppSettings["SettingName"]returns null. It seems that the assembly does not display the configuration file.

What to do to make it work?

+5
source share
4 answers

, , - , ConfigurationManager. , :

ConfigurationManager.AppSettings["SettingName"]

:

var _setting = ConfigurationManager.AppSettings["SettingName"];
// If we didn't find setting, try to load it from current dll config file
if (string.IsNullOrEmpty(_setting))
{
    var filename = Assembly.GetExecutingAssembly().Location;
    var configuration = ConfigurationManager.OpenExeConfiguration(filename);
    if (configuration != null)
        _setting = configuration.AppSettings.Settings["SettingName"].Value;
}

, YourAssemblyName.dll.config, . app.config (, appSetting file), , XPath - .

+4

. (Com, , regasm/codebase), app.config, .

:

string assemblyLoc          = GetType().Assembly.Location;
string configName           = assemblyLoc + ".config";
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configName);

, , .

+3

( dll, vb script, ..)? , PATH evironment...

, VB Script Internet Explorer, ( IE ). , , , , ActiveX, VB Script.

, DLL, - , , .

0

I used satellite response, worked fine in one IIS test environment, but not in another. It turns out that after you set the APP_CONFIG_FILE property, you may need to use reflection to touch the ConfigurationManager class to make changes. I used this function after setting the APP_CONFIG_FILE property:

    private static void ResetConfiguration()
    {
        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);
    }

Other than that, it's probably worth saving the property first and then restoring it when you are done:

string oldConfigName = AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
        //do custom stuff in here
        AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfigName); //re-point to the original configuration.
        ResetConfiguration();
0
source

All Articles