Excel Add-In not loading app.config with service reference config information

后端 未结 3 727
慢半拍i
慢半拍i 2020-12-06 23:18

I\'ve written an app with a service reference to make web services calls to a specific URL and it works great. I want to move this code into an Excel Add-In, but I run into

相关标签:
3条回答
  • 2020-12-06 23:59

    I have had the same problem recently myself and found that the best solution was contained in:

    Change default app.config at runtime

    pointing your config file at:

    AppDomain.CurrentDomain.BaseDirectory + "AssemblyName.dll.config"
    

    where AssemblyName is the name of your addin assembly

    0 讨论(0)
  • 2020-12-07 00:06

    You would need to open it using the ConfigurationManager. You can find your app.config file in the calling assembly path (usually), so you could write a method like this:

    public static Configuration LoadLocalConfigurationFile(string fileName)
    {
        // fileName is the configuration file you want to open
        var configMap = new ExeConfigurationFileMap 
        { 
            ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName)
        };
    
        return ConfigurationManager.OpenMappedExeConfiguration(
            configMap, ConfigurationUserLevel.None);
    }
    
    0 讨论(0)
  • 2020-12-07 00:14

    As @hexboy noted in his comment, the answer is that ExcelDNA requires the config file to be the same name as the "xll" file but with a ".config" extension. Then ExcelDNA will load the configuration automatically and no further code is required.

    Note also that ExcelDNA produces various different files on build. E.g. addin.xll, addin-packed.xll, addin64.xll, addin64-packed.xll where the project assembly is named addin.

    In order for a config file to be picked up, it needs to match the file name that is used for the add-in. E.g. addin-packed.xll.config if using addin-packed.xll as the registered Excel add-in.

    0 讨论(0)
提交回复
热议问题