How can I make a .NET class library read its own configuration file?

pyon picture pyon · Mar 18, 2011 · Viewed 19k times · Source

I have a .NET class library that provides a set of helper functions that are used by several Web Services. This class library must store a single setting, specifically, a connection string, which need not be seen by the Web Services themselves, since they all must query the same datbase.

Unfortunately, .NET provides no means to easily read a DLL's app.config file. The only "easy" solution would be to store the connection string in every single Web Service configuration file, which is completely bollocks.

Normally, I care about code elegance, but this time I really need a solution, even if it is a hack. Is there any way to make a .NET class library have its own configuration?


EDIT: Technically, I could merge all those Web Services into a single Web Service. But, for business reasons (each Web Service will be sold separately), I cannot do that.

Answer

pridmorej picture pridmorej · Mar 27, 2012

A year out of date I know, but I used this method to read each of the settings:

Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
ClientSettingsSection c = (ClientSettingsSection)csg.Sections["Add your section name here, e.g. Your.Namespace.Properties.Settings"];
foreach (SettingElement e in c.Settings)
{
    Debug.WriteLine("SETTING NAME: " + e.Name);
    SettingValueElement v = e.Value;
    Debug.WriteLine("SETTING VALUE: " + v.ValueXml.InnerText);
}

This works on a settings file created in a Class Library Project. The settings file should be named "YourLibrary.dll.config" and then deployed in the library's location. The settings file should have similar content to this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Your.NameSpace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <Your.NameSpace.Properties.Settings>
      <setting name="YourLibrary_WebReferences_YourWebService" serializeAs="String">
        <value>http://localhost:3861/YourWebService.asmx</value>
      </setting>
      <setting name="AnotherSetting" serializeAs="String">
        <value>False</value>
      </setting>
    </Your.NameSpace.Properties.Settings>
  </applicationSettings>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

I haven't needed to read connection strings from the config file, but that should be possible by changing the name of the section group that you get after opening the exe configuration.

The reason why I needed to do this is that I have a User Control which wraps a ActiveX/COM library which is then hosted in IE in an "object" tag. I have got the use of "param" tags working, so I could have used that mechanism to have passed the settings into the User Control, but this method seemed a logical choice at the time. Plus I wasn't going to let this particular problem beat me!

HTH pridmorej :)