Using appsettings in the DAL layer

血红的双手。 提交于 2019-12-21 05:12:24

问题


I have a winforms application in which some of the data is stored in XML files. The location where these XML files should be stored can be configured by the user, and is stored in the AppSettings. All my layers are separate assemblies. Can I access my settings from my DAL assembly, or should I pass this as an argument through all my layers?

When I try to read the settings from my DAL layer, I encounter another problem

        Configuration config = ConfigurationManager.OpenExeConfiguration(
            System.Reflection.Assembly.GetEntryAssembly().Location);
        string dataStorageLocation = config.AppSettings["DataStorageLocation"];

config.AppSettings["DataStorageLocation"] gives a compilation error: System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty] is inaccessible due to its protection level. Why is that?

Can someone put me on the right track? Thanks.


回答1:


You need to use config.AppSettings.Settings["DataStorageLocation"]. See the MSDN documentation for a sample.

Alternatively, and IMHO better, you could use System.Configuration.ConfigurationManager.AppSettings[name] to access the AppSettings of the host application. This is probably more flexible than your technique, as it will also work if your DAL assembly is, for example, hosted in a service tier on IIS. Accessing configuration information from the host application's configuration file directly in this way is perfectly accceptable, and generally better than passing configuration information down through the layers.




回答2:


The AppSettings are accessible to any assembly loaded by the calling process, so you will have no problems accessing them with any assembly you load.



来源:https://stackoverflow.com/questions/1995012/using-appsettings-in-the-dal-layer

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