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