A practical guide. Using a ConfigurationSection without loading it via a GetSection call

I would like to load a specific ConfigurationSection, but the way CLR loads the assemblies is giving me some problems:

The definition of My CustomConfigurationSection refers to a specific assembly that cannot be found in the general assembly loading process as I am using an external tool that basically loads my assembly, detects some information about it through reflection, and then tries to "install" It. Very similar to installutil when trying to install a windows service.

I'm going crazy because the ConfigurationManager is trying to find the correct assembly for my ConfigurationSection at the location of the original process. I know this for sure as I am using SysInternals Process Monitor. Can anyone provide some workarounds or directions?

Thank!

+1


source to share


4 answers


If you know the path to your assembly, try ConfigurationManager.OpenExeConfiguration (exePath).



+1


source


If your assembly is required to deserialize the custom configuration section, but the CLR cannot find the assembly, then I think you are out of luck (or am I missing the problem?).



Is there a way you can get the CLR to find your assembly (maybe this could be the hint path)? If not, you might be better off using a separate XML file for this data rather than using app.config / web.config.

0


source


Why are you trying to access the config section before your assembly (which defines the config section) has been loaded? Are you using the config section to determine where your assembly is located? If so, you are playing with circular links.

The code for defining a custom configuration section can be very self-contained. It could be its own assembly. I would suggest splitting this code into your own assembly and installing it in the GAC or at runtime. I don't know why you need an external tool to load the code needed to read the custom config section.

0


source


I faced a similar problem. Several DLLs are loaded into the main application dynamiccaly. Some of these dlls require a configuration file and I use the default ConfigurationManager to handle this. I can successfully get the correct file (based on the dll name marked ".config") and use the settings from AppSettings and ConnectionStrings.

Now I am trying to load a custom config section. The runtime is complaining about a section type that is not found in the DLL. I have specified the correct dll in the config file (in the configSections entry) and I know the dll is loading because this dll is actually a plugin. But still; it looks like the runtime only uses types from the GAC / bin directories to find configuration sections.

So, in short: I am trying to load a custom config section that is listed in the same DLL as the code that is trying to load it, but it doesn't work.

0


source







All Articles