Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 315
pyon Avatar asked Mar 18 '11 17:03

pyon


People also ask

Can a class library have an app config file?

Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App. config file.

Can we add app config file in Class Library C#?

To add an application configuration file to a C# projectIn the middle pane, select the Application Configuration File template. Select the Add button. A file named App. config is added to your project.

Does app config get compiled into DLL?

If you don't want to expose values, make sure you have an app. config with deployment values (empty, 0, or something). The values WILL be compiled into the DLL as default values.


2 Answers

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 :)

like image 147
pridmorej Avatar answered Nov 12 '22 11:11

pridmorej


I think you're looking for:

ConfigurationManager.OpenExeConfiguration(string exePath)

or

ConfigurationManager.OpenMappedExeConfiguration(
    new ExeConfigurationFileMap() { 
        ExeConfigFilename = path + "app.config" 
    }, ConfigurationUserLevel.None);

Which returns a Configuration object. MSDN doc on ConfigurationManager

Try this question for how to get the DLL path.

like image 30
s_hewitt Avatar answered Nov 12 '22 11:11

s_hewitt