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

纵饮孤独 提交于 2019-11-26 21:59:03

问题


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 this problem:

Unhandled Exception Message: Could not find endpoint element with name 'ConnectivityHttpsSoap12Endpoint' and contract 'Connectivity.ConnectivityPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

The problem is that my app.config (w/the service reference info) isn't being loaded because Excel is calling my class library, and the app.config of the calling application would need to have the service reference added to it. But can that be done with Excel? Better yet, is there a way to just load my app.config from code?


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/13199432/excel-add-in-not-loading-app-config-with-service-reference-config-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!