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
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
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);
}
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.