473,473 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ConfigurationSection and Class Library

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?

Apr 27 '06 #1
7 13010
Even the ConfigurationManager.OpenMappedExeConfiguration doesn't work
afterall. It's loading the machine config instead. Am I forgetting
something. I used the sample from MSDN:
http://msdn2.microsoft.com/en-us/lib...on(VS.80).aspx

Apr 27 '06 #2
Try renaming the configuration file to <executableName>.config

Apr 27 '06 #3
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(fi leMap,
ConfigurationUserLevel.None);

Apr 27 '06 #4
Hi,
"Alexander van Doormalen" <av*********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 27 '06 #5
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

Apr 27 '06 #6
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(fi leMap,
ConfigurationUserLevel.None);

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

Apr 27 '06 #7
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.

Apr 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: syd | last post by:
In my project, I've got dozens of similar classes with hundreds of description variables in each. In my illustrative example below, I have a Library class that contains a list of Nation classes. ...
0
by: Jérôme Piquot | last post by:
Hi, When i want to save configuration settings i have a "ConfigurationSection properties cannot be edited when locked" Exception. StackTrace " at...
0
by: Liam | last post by:
I'm using VS 2005 and I have a custom ConfigurationSection class that works perfectly. However, when I reference it from my test project it works fine for all of the properties except when they...
0
by: tony | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. In this user control we have a class...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
4
by: Dabbler | last post by:
I'm trying to setup a email info config section like: <contactForm> <email> <add key="toaddress" value="he@hishost.com;" /> <add key="toaddress" value="she@herhost.com;" /> <add key="toaddress"...
0
by: Michael | last post by:
Hi, I would like to know if it is possible to use the ConfigurationSection class to modify user settings. I have created custom configuration classes that derive from ConfigurationSetion and...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
1
by: =?Utf-8?B?UGhpbGlw?= | last post by:
I am using the ConfigurationSection, ConfigurationElement, ConfigurationElementCollection base classes for purposes of reading a customized web.config Configuration section.....and I then use the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.