【发布时间】:2010-12-13 13:14:41
【问题描述】:
我很难访问配置文件中的自定义配置部分。
正在从作为插件加载的 .dll 读取配置文件。我使用Configuration Section Designer VS 插件创建了配置和必要的代码。
命名空间是“ImportConfiguration”。 ConfigurationSection 类是“ImportWorkflows”。程序集是 ImportEPDMAddin。
xml:
<configSections>
<section name="importWorkflows" type="ImportConfiguration.ImportWorkflows, ImportEPDMAddin"/>
</configSections>
每当我尝试读取配置时,都会收到错误消息:
为 importWorkflows 创建配置节处理程序时出错:无法加载文件或程序集“ImportEPDMAddin.dll”或其依赖项之一。系统找不到指定的文件。
dll 不会与可执行文件位于同一目录中,因为加载插件的软件会将 dll 及其依赖项放在它自己的目录中。 (我无法控制。)
我将单例实例的代码编辑为以下内容:
string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
path = path.Replace("file:///", "");
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);
return configuration.GetSection(ImportWorkflowsSectionName) as ImportConfiguration.ImportWorkflows;
我也尝试过使用简单的 NameValueFileSectionHandler,但我收到一个异常,说它无法加载文件或程序集“系统”。
我已经阅读了许多博客文章和文章,听起来可以为 dll 读取配置文件,但我就是无法让它工作。有任何想法吗?谢谢。
【问题讨论】:
-
您是否也将
ImportEPDMAddin.dll.config复制到同一位置? -
配置肯定在那里,因为我尝试使用另一个类的 DictionarySectionHandler 并且有效。