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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Something like this:

var exeMap = new ExeConfigurationFileMap {ExeConfigFilename = @"C:PathToYourFile"};
Configuration myConfig = ConfigurationManager.OpenMappedExeConfiguration(
    exeMap, ConfigurationUserLevel.None
);

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x