Search code examples
c#.net-4.0app-configcomvisible

ComVisible .NET assembly and app.config


  • I have .NET assembly with some classes marked as ComVisible
  • This assembly is registered with regasm /codebase "assembly_path"
  • I have app.config name (actually - MyAssemblyName.dll.config) which are in assembly's folder
  • I access to appSettings in my assembly through ConfigurationManager.AppSettings["SettingName"]
  • I have VBScript file which creates my COM object through CreateObject("...")
  • When object is created (from VBScript), ConfigurationManager.AppSettings["SettingName"] returns null. It looks like assembly doesn't see config file.

What should I to do to make it workable?


Solution

  • One possible way, as Komyg said, is to read config file directly, instead of using ConfigurationManager's embedded behavior. For those, who will have the same problem: instead of

    ConfigurationManager.AppSettings["SettingName"]
    

    you can use:

    var _setting = ConfigurationManager.AppSettings["SettingName"];
    // If we didn't find setting, try to load it from current dll's 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;
    }
    

    This way you will always use read settings from file YourAssemblyName.dll.config which lays in your assembly's folder. It will allow you to use also another features for app.config (like appSetting's file attribute), which will no be available if you will use XPath or something like this.