我有一个托管.Net组件的COM +服务器,它实现了ServicedComponent。
COM +服务器需要访问已定义自定义配置部分的配置文件。
我可以使用以下代码加载配置:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"%MY_FOLDER_WITH_ALL_DLLS%\MyComServer.dll.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
// All is fine until the next line:
MyCustomSettings customSettings = (MyCustomSettings)tempConfiguration1.GetSection("customSettings");
System.InvalidCastException:无法将类型为“System.Configuration.DefaultSection”的对象强制转换为“MyProject.MyCustomSettings”
以下是我在配置文件中声明自定义配置部分的方法:
<configSections>
<section name="MyProject.MyCustomSettings" type="MyProject.MyCustomSettings, MyProject, Version=1.0.3322.1077, Culture=neutral, PublicKeyToken=176fc8b9840b0b09"/>
</configSections>
这种情况确实会返回一个DefaultSection对象,因为我期待一个CustomSettings对象,所以它似乎没什么用处。
请注意,MyProject名称很强。
一个选项是在GAC中安装MyProject.dll程序集,但出于组织原因,此解决方案没有吸引力。
还有其他建议吗?
如何从DLLHost中运行的进程加载给定程序集的配置文件中的自定义配置部分?
感谢。
答案 0 :(得分:0)
我自己浪费了几个小时来解决这个问题。最后我通过移动&lt; configSection&gt;解决了它。在右下方&lt; configuration&gt;。所有这些时间我都有其他配置元素&lt; configSection&gt;。