473,729 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

app.config in Class Libraries

Hello!

I created a Class Library project in VS2005. Then, using VS, I was able to
add a connection string to the project settings, which automaticaly created
an app.config file for me.
If I try to access the configuration using
System.Configur ation.Configura tionManager, I get a NullException.

The code:
string connectionStrin g =
ConfigurationMa nager.Connectio nStrings["myConnectionNa me"].ConnectionStri ng;

After that, I created a Windows Application Project and went through the
same steps and it worked, with a little twist: When I debug the application
it seems that the ConfigurationMa nager finds TWO connection strings, the one
that I create and one called "LocalSqlServer ", which I have no idea where
it's located since it's not into the app.config file.
My questions:
1. Is it correct to assume that Class Libraries do not work with app.config
files on their own? If so, why would VS2005 allow for the creation of the
same.
2. Does the configuration manager automaticaly read configuration info from
any other file? (Where a "LocalSqkConnec tion" would be).

Thanks,
Fernando
Aug 24 '06 #1
3 18532
Fernando,

1. DLLs doesn't have application configuration fiels. As the names suggest
this configuration file is menat to be used (read) by an application; the
dll is not an application.
VS creates config files for dlls, but this file is meant to be given proper
name and copied in the application BIN folder where the executable that uses
the dll is.

From MSDN:
"When you build your project, the development environment automatically
creates a copy of your app.config file, changes its file name so that it has
the same file name as your executable, and then moves the new .config file
in the bin directory."

2. I guess this connection string comes from the machine.config file which
is like default configuration for all .NET applications running on the local
machine.
--
HTH
Stoitcho Goutsev (100)

"Fernando Chilvarguer" <fe******@no-spam-impex.comwrote in message
news:u7******** ******@TK2MSFTN GP04.phx.gbl...
Hello!

I created a Class Library project in VS2005. Then, using VS, I was able to
add a connection string to the project settings, which automaticaly
created an app.config file for me.
If I try to access the configuration using
System.Configur ation.Configura tionManager, I get a NullException.

The code:
string connectionStrin g =
ConfigurationMa nager.Connectio nStrings["myConnectionNa me"].ConnectionStri ng;

After that, I created a Windows Application Project and went through the
same steps and it worked, with a little twist: When I debug the
application it seems that the ConfigurationMa nager finds TWO connection
strings, the one that I create and one called "LocalSqlServer ", which I
have no idea where it's located since it's not into the app.config file.
My questions:
1. Is it correct to assume that Class Libraries do not work with
app.config files on their own? If so, why would VS2005 allow for the
creation of the same.
2. Does the configuration manager automaticaly read configuration info
from any other file? (Where a "LocalSqkConnec tion" would be).

Thanks,
Fernando

Aug 24 '06 #2
Hi,

You cannot,

You have two options:

Use the app.config file from the app and include your config settngs there
together with those of the app hosting the dll.

Use a different file and parse it in the DLL. I have used this approach
several times, it's not the best but it does work and you keep separated
both configurations settings.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Fernando Chilvarguer" <fe******@no-spam-impex.comwrote in message
news:u7******** ******@TK2MSFTN GP04.phx.gbl...
Hello!

I created a Class Library project in VS2005. Then, using VS, I was able to
add a connection string to the project settings, which automaticaly
created an app.config file for me.
If I try to access the configuration using
System.Configur ation.Configura tionManager, I get a NullException.

The code:
string connectionStrin g =
ConfigurationMa nager.Connectio nStrings["myConnectionNa me"].ConnectionStri ng;

After that, I created a Windows Application Project and went through the
same steps and it worked, with a little twist: When I debug the
application it seems that the ConfigurationMa nager finds TWO connection
strings, the one that I create and one called "LocalSqlServer ", which I
have no idea where it's located since it's not into the app.config file.
My questions:
1. Is it correct to assume that Class Libraries do not work with
app.config files on their own? If so, why would VS2005 allow for the
creation of the same.
2. Does the configuration manager automaticaly read configuration info
from any other file? (Where a "LocalSqkConnec tion" would be).

Thanks,
Fernando

Aug 24 '06 #3
Thanks, I followed the directions from your answers and it worked fine.

BUT

I once again got confused once I introduced a Typed DataSet to the Class
Library.

Once I added the Typed DataSet, VS automatically added an app.config file to
my Class Library and put the database connection string in it.

After compiling the whole application, I can see that a file called
ClassLibrady.Dl l.Config got created into the /BIN/DEBUG directory.

Since it's the only place where the connection string for the Typed DataSet
has been stored, I have to assume that somehow my class library is reading
the configuration from it's own app.config (renamed and copied as
ClassLibray.dll .config) and NOT the app.config from my application. Could
you clarify me on that?

I'm confused.

Thanks,
Fernando

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:eg******** ******@TK2MSFTN GP06.phx.gbl...
Fernando,

You are right, class libraries do not work with app.config files for
them. Rather, they should read the settings that are in the app.config
file for the executable that references them.

You should only have one configuration file for your app. If the
libraries need config info to configure itself, then it should be placed
in the app.config file for the exe. The configuration framework will do
the rest.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Fernando Chilvarguer" <fe******@no-spam-impex.comwrote in message
news:u7******** ******@TK2MSFTN GP04.phx.gbl...
>Hello!

I created a Class Library project in VS2005. Then, using VS, I was able
to add a connection string to the project settings, which automaticaly
created an app.config file for me.
If I try to access the configuration using
System.Configu ration.Configur ationManager, I get a NullException.

The code:
string connectionStrin g =
ConfigurationM anager.Connecti onStrings["myConnectionNa me"].ConnectionStri ng;

After that, I created a Windows Application Project and went through the
same steps and it worked, with a little twist: When I debug the
application it seems that the ConfigurationMa nager finds TWO connection
strings, the one that I create and one called "LocalSqlServer ", which I
have no idea where it's located since it's not into the app.config file.
My questions:
1. Is it correct to assume that Class Libraries do not work with
app.config files on their own? If so, why would VS2005 allow for the
creation of the same.
2. Does the configuration manager automaticaly read configuration info
from any other file? (Where a "LocalSqkConnec tion" would be).

Thanks,
Fernando


Aug 24 '06 #4

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

Similar topics

13
3019
by: Maxim Khesin | last post by:
I want to have a config file with my python proggie, satisfying the following requirements: 1) support key->(value, default) 2) simple and intuitive to read and edit 3) easyly readable into a python datastructure (a dictionary?) 4) not requiring any heavy libraries needed (I am distributing my proggie as a py2exe executable and do not want to bloat the size) can you guys suggest some format for this? thanks, max
3
1818
by: Simon | last post by:
Hi everyone, I really need help with the following: It's common in app development to create numerous projects under one solution that as a whole combine to become your final application. For example I have a solution that has four subprojects - 3 class libraries and 1 windows forms app. Q - Some of these projects - both class library and win form application
3
2224
by: grs | last post by:
Can a class library have a app.config file. Reason for asking is that the microsoft application blocks all read from myApp.exe.config. How can you use the application blocks if you do not have an app.config file. Wish someone from microsoft would answer this, I am at a loss. thanks grs
12
2256
by: CodeRazor | last post by:
Hi, I am building a class library and want to store a connection string in a configuration file. I've read around and it appears that this is not possible because "by design, class libraries don't have their own config file - they read the config files from the application they are housed in, such as a WinForm app or ASP.NET site "
7
21168
by: A.M-SG | last post by:
Hi, We have a class library application that needs to read some application settings from it's own app.config file. I assume that a ClassLibrary.DLL can have a app.config file, but during
1
1154
by: Erik J Sawyer | last post by:
Is there any documentation on using config files with multiple assemblies? For example, I have a class library installed to the GAC. This is then used in several ASPX pages. If the class library requests settings from a config file, whose will it get: - the app.config that was built with the library (but doesn't appear to have been installed to the GAC) - the web.config of the calling application
4
5023
by: kalyankp78 | last post by:
Hi, I have a question about config files. I have the following in my application.. 1. ASP.NET web application with a web.config 2. C# class library (BL and DAL) with an App.config 3. Two windows Console Applications with their own App.Config. Now I try to retrieve a value in the DAL using this code.
8
1652
by: =?Utf-8?B?Y2FsZGVyYXJh?= | last post by:
Dear all, I am building a set of libraries working in a n tiers architecture Some of those libraries use common configuration settings like the database connection string and some others. I can imgaine that I can get one config file per buisness library. What is teh best way and where to store configuration files ? Should I get only one with everything in and located in a particular common
8
3181
by: Bill McCormick | last post by:
<!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> I assume "libraries" here to mean DLL's. If that's the case, is there any way to supply both ends of a service (client and host) with the code for the class that describes the contact WITHOUT a duplication in source code? Thanks.
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.