Where to place app.config file

E

Ed_P.

Hello,

I have the following scenario: I have a solution with 4 projects
Project1 = MainWindow (Windows EXE)
Project2 = PresentationLayer (DLL)
Project3 = BusinessLayer (DLL)
Project4 = DataLayer (DLL)

I have referenced all of the other projects in the first project so that
I could use the objects I've created in them. However, I am running in
to the following situation. In the DataLayer Project, I have created a
class called Database. This database class has a static method called
Connection which retruns a SqlConnection object.

I have created an app.config file for the data layer project and have
added the key called "SqlConnection" for my sql server, here is an exmple:

<appSettings>
<add key="SqlConnection" ...>
</appSettings>

In the DataLayer Static method, I am calling the following:
public static SqlConnection Connection()
{
string sqlConnection = ConfigurationSettings.AppSettings["SqlConnection"];

//creating of sql connection object
SqlConnection connection = new SqlConnection(sqlConnection);

return(connection)
}

However, when I call this in my code, I get an error stating that the
connection string has not been initialized. One thing I've noticed is
that I've gone to Project1's debug folder (where all of the aseemblies
are) and I notice that the app.config file for the data layer project is
not there.

I am trying to avoid to create the app.config file for Project1 and
passising it's connection string from there. can anyone tell me what I
can do to have this work the way I want it to work?

Any advice or tips will be greatly appreciated.
 
J

james

app.config is for the .exe not for the dll frameworks. You should pass the
connection string into your Database class in its contructor. Then your
main app loads the app.config, gets the connection info and passes it to the
DataLayer. The point here is that by doing it this way you allow your
DataLayer to be re-used by other apps that pass in different connection
strings.

JIM
 
W

Wayne

If you create the app.config for project1 your DLL will just read that APP
config. Class libraries read the app.configs of the process they are loaded
into. So there would be no need to pass the connection string from the EXE
to the DLL. I believe this was done because you may actually use that DLL
with multiple EXE's.

I saw something on one of the group that showed how to get the DLL to read
from a different config, you may try searching groups.google.com if you
don't like the idea of using one App.config.

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
N

Nicholas Paldino [.NET/C# MVP]

Ed,

You should not have an individual application configuration file for
your dll. Basically, the app config file is meant to be the configuration
for the application as a whole, which your component is a part of. Because
of this, you will have to have an application configuration file for the
entry point for the CLR (in this case, your main executable).

If you want to have a configuration file for your assembly, you are
going to have to code it up yourself, but I would recommend against it, as
it doesn't make much sense (why have multiple areas where the dll can be
configured from an application standpoint).

If the services offered by your assembly are truly so universal, and
require it's own configuration, then you should consider making it a
service, as that is more in line with what it is providing, as well as the
needs it has for configuration.

Hope this helps.
 

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