How to access a custom config from my class library application?
开发者_如何学编程I have a custom config in my Infrastructure Project, but when my app start only my web.config is recognized.
I don't want to place the configuration of this custom config file in my web.config because this configuration is responsability for Infrastructure Layer.
How I use this custom config from another project in my web project?
The previous answers to this question are failing to inform you of a critical point.
.NET is designed to have a single configuration process for each AppDomain
. All class libraries will use the configuration file of the application which calls them. In your case, your class library will use the web.config. If your class library were being used from a console application, then it would use the application.exe.config file.
When you think about it, this is the only thing that makes sense. If your class library is used from two separate applications, then it will have two separate configurations. These configurations must be managed on behalf of the calling application.
Something like this:
var exeMap = new ExeConfigurationFileMap {ExeConfigFilename = @"C:\Path\To\Your\File"};
Configuration myConfig = ConfigurationManager.OpenMappedExeConfiguration(
exeMap, ConfigurationUserLevel.None
);
Assuming that the infrastructure project is a class library, have you considered creating properties to expose the app.config (?) settings to the front-end?
Here is an example of what I mean. Keep in mind that this is just an example. It's a property that demonstrates how to expose a config value through a property. In your case, the property would retrieve a value from your custom config file.
public string GetConfigurationValue(string Key)
{
return GetValueFromConfiguration(Key);
}
One thing I'm wondering is why you're creating a custom configuration file rather than just creating a configuration section that you can integrate into the app.config file. It seems like you're making things much more difficult than they need to be.
精彩评论