views:

324

answers:

4

if you have a class library project that acts as ur DAL and it has an App.Config file with connectionstrings, how can I force it to use that config file? It keep getting values from the web.config in my Web Application project.

The DAL project uses LinqToSql. When I instansiate a DataContext object in my Web Application, from the referenced DAL Class Library project, can I make it use it's app.Config connectionstrings? It seems to ignore this file and tries to pick up connectionstrings from the web.Config connectionstrings node. There are no connectionstrings present there.

Any help is appreciated. A colleague mentioned making the app.Config in the DAL and embedded resource. Does that sounds like a good idea?

Thanks, ~ck in San Diego

+3  A: 

Web applications always use web.config. Desktop applications always use app.config.

Eric J.
If you need to override it, I'd move the configuration settings either to web.config or to an XML file that you can manage and deploy manually.
Jeff Siver
+1  A: 

how can I force it to use that config file? It keep getting values from the web.config in my Web Application project.

You can't. If you use the System.Configuration classes, they will always pull from the active application's .config file (app.config for executables, web.config for asp.net websites).

Workarounds include using file i/o for reading your settings out (as opposed to the System.Configuration namespace) or putting your DAL configuration information in the appropriate .config file (the more common choice).

Ken Browning
A: 

I'm not sure, but take a look at that:

using System.Configuration;

ExeConfigurationFileMap Map = new ExeConfigurationFileMap();

Map.ExeConfigFilename = FileName;

Configuration Conf = ConfigurationManager.OpenMappedExeConfiguration(Map, ConfigurationUserLevel.None);

AppSettingsSection section = (AppSettingsSection)Conf.GetSection("???");
uli78
A: 

Here's how I think of a *.config file. Say you have a method in your DAL:

DoSomething(connectString, SqlDialect, businessObject)

Since connectString and SqlDialect don't change with every call to this method, it would be nice to be able to remove those parameters and get those through some other means.

Well, *.config files are there for that reason--they are not only environment-specific, they are also app-specific. It's so your Web app can say, "Hey everybody, connectString = "..." and SqlDialect = "...", for every method call, until I say otherwise."

Let's say you want one app to log into SQL with one set of credentials, and another app to use another set of credentials (with different perms if necessary) so that the DBA can keep track of which app is doing what (if he/she so chooses). Well, *.config files make this happen.

That's why the app that you're running is the one that provides the *.config file. So just cut all the contents from your DAL's app.config file and paste it into your Web.config file, then delete App.config. You should be good-to-go after that.

apollodude217