app.config in Class Libraries

  • Thread starter Fernando Chilvarguer
  • Start date
F

Fernando Chilvarguer

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
 
S

Stoitcho Goutsev \(100\)

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.
 
I

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

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.
 
F

Fernando Chilvarguer

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 said:
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]
- (e-mail address removed)

Fernando Chilvarguer said:
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
 

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