Search code examples
c#service-referenceconfigurationsection

Issue with a DLL reading a ConfigurationSection when called through Service Reference


I have a project (A) referencing a Service (B) via. a Service Reference.

That Service (B) has a reference to a DLL (lets call it Business.dll)

That DLL has it's own accommodating app.config which has some configuration that I would assume would be easily read whether being called internally (as a console app) or externally from Service (B).

Currently, this isn't working. The app.config (or, more specifically, the Business.dll.config file) is not being read at all, and:

(BusinessConfigurationSection)ConfigurationManager.GetSection("GroupName/SectionName");

is always null when called from project (A). Can I not save the Business.dll.config within the bin directory of Service (B), or am I doing something that simply is not possible? Is there a better way that I should be doing this?

Thanks.


Solution

  • You can load a specific config file like this :

    Configuration config;
    ExeConfigurationFileMap ecfm = new ExeConfigurationFileMap();
    ecfm.ExeConfigFilename = <your_config_file_path>;
    
    config = ConfigurationManager.OpenMappedExeConfiguration(ecfm, ConfigurationUserLevel.None);
    var mySection = (BusinessConfigurationSection)config.GetSection("GroupName/SectionName");