views:

296

answers:

1

Hi,

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.

A: 

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>.

Hope it still helps, after all this time.

Patrick Huizinga