ConfigurationSection and Class Library

  • Thread starter Alexander van Doormalen
  • Start date
A

Alexander van Doormalen

Is it possible to load data from a configuration file within a assembly
(class library)? The hosting exe file is something completely different
which isn't gonna load my config file.

I already added a App.config to my project and its copied to my output
directory on build. Only it seems its not automatically loaded (is this
only possible with executables or asp.net?).

Normally you can call the configurationmanager to get a custom section
(or appsetting).
I use this to get a section.
ConfigurationManager.GetSection("samplesection"). Only this returns
null in my case. Appsetings is also empty.

I know its also possible with
ConfigurationManager.OpenMappedExeConfiguration and then giving it a
complete path to the config file. Only this requires a constant value
telling me the name of the configuration file which I preffer not. I
rather have it loaded automatically (if possible offcourse).

Any thoughts?
 
A

Alexander van Doormalen

Then I get the following exception:

System.InvalidOperationException: An unexpected error occurred in
'ClientConfigurationHost::Init'.
at
System.Configuration.ClientConfigurationHost.Init(IInternalConfigRoot
configRoot, Object[] hostInitParams)

Configfile is named: <executableName>.exe.config
Code:

string path = Assembly.GetCallingAssembly().CodeBase;
string directoryPath = Path.GetDirectoryName(path);
string processName = Process.GetCurrentProcess().ProcessName;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(directoryPath, processName +
".exe.config");

ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Alexander van Doormalen said:
Is it possible to load data from a configuration file within a assembly
(class library)? The hosting exe file is something completely different
which isn't gonna load my config file.

Nop, a dll has no a config , you can check the archives for several threads
about this very same problem.

I already added a App.config to my project and its copied to my output
directory on build. Only it seems its not automatically loaded (is this
only possible with executables or asp.net?).

Yes, I think it's copied cause by default it's marked as content. the
framework will not use it at all.

Any thoughts?

You can use your own implementation of a config. you have two options if you
want to use a config file:
1- use a similar file that the one that .net use, this is a XML file that
you can easily parse, you can store the values in a hash table
2- use a text file (this is my preferred option) in the form key=value ,
read it and parse and either keep the values in a hashtable or store them in
predefined variables.
 
A

Alexander van Doormalen

Parsing it myself is 1 thing I don't wanna do anymore. The new
configuration options of .net 2.0 are great and they need to work.
Key=value method is old school (ini) and its not what I need.
ConfigSections is the way to go for my solution.

So still need a solution to fix the problem posted earlier and I'll be
happy
 
A

Alexander van Doormalen

Fixed the exception. I think its a bug within the framework. Its caused
by the file:// (uri format) that Assembly.CodeBase returns.

Solution:

Uri path = new Uri(Assembly.GetCallingAssembly().CodeBase);
string directoryPath = Path.GetDirectoryName(path.LocalPath);
string processName = Process.GetCurrentProcess().ProcessName;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(directoryPath, processName +
".exe.config");

ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);

Anyways if someone knows how to autoload the Configuration that would
be even better. For now this will do I guess. Thnx!
 
A

Alexander van Doormalen

Guess my problem isn't solved yet.

My second problem is that the config file now loads under the host's
appdomain. Any ConfigurationSection types defined in the config file
cannot be found. Why? because the host executable is somewhere else
located on the disk.

Files can't be put in the GAC because they are extensions which need to
be in a specific directory which the host monitors and loads from.

Is there an other way to let the runtime know where it needs to look?

Config part I'm talking about:

<section name="somename" type="namespace.classname, assemblyname,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

So when loading the configuration file it tries to load
'namespace.classname' from 'assemblyname' but cannot find the assembly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top