具有自定义ConfigurationSection的COM +服务器配置
我有一个COM +服务器托管.Net组件实现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”
以下是我在配置文件中声明自定义配置部分的方法:
这种情况确实会返回一个DefaultSection对象,因为我期待一个CustomSettings对象,所以它看起来并没什么用处。
请注意,MyProject名称很强。
一个选项是在GAC中安装MyProject.dll程序集,但出于组织原因,此解决方案没有吸引力。
还有其他建议吗?
如何从DLLHost中运行的进程加载给定程序集的配置文件中的自定义配置节?
谢谢。
我自己浪费了几个小时来解决这个问题。 最后,我通过将