473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.Configuration.ConfigurationManager, I get a NullException.

The code:
string connectionString =
ConfigurationManager.ConnectionStrings["myConnectionName"].ConnectionString;

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 ConfigurationManager 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 "LocalSqkConnection" would be).

Thanks,
Fernando
Aug 24 '06 #1
3 18513
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**************@TK2MSFTNGP04.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.Configuration.ConfigurationManager, I get a NullException.

The code:
string connectionString =
ConfigurationManager.ConnectionStrings["myConnectionName"].ConnectionString;

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 ConfigurationManager 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 "LocalSqkConnection" 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**************@TK2MSFTNGP04.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.Configuration.ConfigurationManager, I get a NullException.

The code:
string connectionString =
ConfigurationManager.ConnectionStrings["myConnectionName"].ConnectionString;

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 ConfigurationManager 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 "LocalSqkConnection" 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.Dll.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.comwrote in
message news:eg**************@TK2MSFTNGP06.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.com

"Fernando Chilvarguer" <fe******@no-spam-impex.comwrote in message
news:u7**************@TK2MSFTNGP04.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.Configuration.ConfigurationManager, I get a NullException.

The code:
string connectionString =
ConfigurationManager.ConnectionStrings["myConnectionName"].ConnectionString;

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 ConfigurationManager 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 "LocalSqkConnection" 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
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...
3
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...
3
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...
12
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...
7
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...
1
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...
4
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...
8
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...
8
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. --> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
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...

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.