By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,758 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,758 IT Pros & Developers. It's quick & easy.

app.config in Class Libraries

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.