Configuration madness, I have an app.Config but my object looks to web.Config

泄露秘密 提交于 2019-12-13 18:50:16

问题


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


回答1:


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




回答2:


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




回答3:


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("???");



回答4:


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.



来源:https://stackoverflow.com/questions/1303202/configuration-madness-i-have-an-app-config-but-my-object-looks-to-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!