COM+ Server configuration with custom ConfigurationSection

时光怂恿深爱的人放手 提交于 2019-12-23 03:57:17

问题


I have a COM+ server hosting a .Net component which implements ServicedComponent.

The COM+ server needs to access a configuration file in which a custom configuration section has been defined.

I can load the configuration fine with the following code:

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: Unable to cast object of type 'System.Configuration.DefaultSection' to type 'MyProject.MyCustomSettings'

Here is how I declared the custom config section in the configuration file:

<configSections>
    <section name="MyProject.MyCustomSettings" type="MyProject.MyCustomSettings, MyProject, Version=1.0.3322.1077, Culture=neutral, PublicKeyToken=176fc8b9840b0b09"/>
</configSections>

This case indeed returns a DefaultSection object which does not seem to be of much use since I was expecting a CustomSettings object.

Please note that MyProject is strongly named.

An option is to install the MyProject.dll assembly in the GAC, but for organisational reasons, this solution is unappealing.

Any other suggestion?

How can I load a custom configuration section from a given assembly's configuration file from a process running in DLLHost?

Thanks.


回答1:


I myself wasted a few hours with this problem. In the end I solved it by moving <configSection> to right below <configuration>. All this time I had other configuration elements above <configSection>.



来源:https://stackoverflow.com/questions/516261/com-server-configuration-with-custom-configurationsection

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