views:

243

answers:

3

I've got a bunch of DLL projects that I'm pulling into my application, each contains their own Settings.settings/app.config. When I compile the app and run for debugging, everything works just fine, but come deployment time I can't get my DLLs to read their own settings files.

I've been doing some reading and it has become apparent that there's a couple of methods to getting each dll to read its own configuration - one is to dedicate a .dll.config to the library and the other is to embed the dll's configuration in the process.exe.config.

I'm having significant issues trying to implement either and I wondered if anyone has any good docs on this - there appears to be a shortage on the Net.

I'd like a separate .dll.config for each of the libraries if possible, but in a pinch, getting each of my libraries to read their own section of the process.exe.config will do.

Can anyone point me in the right direction because I'm so close to rolling this application out but this stumbling block is causing me a significant headache.

Edit: When I merge the configuration files, I start getting TypeInitializer exceptions when I initialize objects withing my libraries. This is likely just me being retarded, but does someone have a working example of a merged config file and some basic demonstrative code for reading it from multiple assemblies?

A: 

Have each class library define configuration settings in a custom ConfigurationSection.

Then add custom section handlers to your process.exe.config file.

This MSDN article is pretty comprehensive in its explanation, with examples in both VB and C#.

Ian Nelson
I've had to do this in the past - that's a LOT of work considering the libraries and main assembly are this close to completion.
BenAlabaster
Yeah, it does involve writing some pretty repetitive code!Dmitryr has created a tool to generate the config sections from a fragment of a XML configuration, which might help:http://blogs.msdn.com/dmitryr/archive/2005/10/02/476245.aspx
Ian Nelson
+2  A: 

What are the "significant issues" you encountered? I started with embedding the dll's config in the exe's config, which worked, but was cumbersome. I now have all the config stuff in one dll project. The only thing I needed to do to make that work (besides copying the settings over) was to change the Settings class to be public.

Here's an example of a merged app.config that works:

  <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
    <configSections>
      <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="SharedConfig.Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- Begin copy from library app.config -->
        <section name="SharedConfig.Library.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- End copy from library app.config -->
      </sectionGroup>
    </configSections>
    <applicationSettings>
      <SharedConfig.Client.Properties.Settings>
        <setting name="Bar" serializeAs="String">
          <value>BarFromClient</value>
        </setting>
      </SharedConfig.Client.Properties.Settings>
      <!-- Begin copy from library app.config -->
      <SharedConfig.Library.Properties.Settings>
        <setting name="Bar" serializeAs="String">
          <value>BarFromLibrary</value>
        </setting>
      </SharedConfig.Library.Properties.Settings>
      <!-- End copy from library app.config -->
    </applicationSettings>
  </configuration>
Daniel Pratt
I attempted this in the first instance because I thought it would be a quick (if a little dirty) fix to the issue and I could get this rolled out. But the minute I merged the all the config files, I started getting TypeInitializer exceptions trying to initialize my objects within my libraries.
BenAlabaster
Then I went to attempting to have dedicated .dll.configs and I can't get the libraries to reference their own files, I know it's likely something simple I'm doing wrong - but even OpenExeConfiguration doesn't seem to want to do the because the Settings.settings files don't seem to update
BenAlabaster
Based on my experience, I'm not surprised you didn't get very far with each .dll loading its own config. One the other hand, your issue with the first scenario has me puzzled. Maybe something about the way you merged them?
Daniel Pratt
Which leads me to wonder what I did wrong - I literally merged the two - a la diff.
BenAlabaster
I can imagine that diff may not have "done the right thing". I set up a simple solution to verify this and it does indeed work. I'm going to edit my post to include the merged app.config.
Daniel Pratt
Thanks - I appear to have got it working :) Much appreciated
BenAlabaster
A: 

See http://stackoverflow.com/questions/634439/if-app-config-for-a-dll-should-be-in-the-main-config-what-do-we-do-with-wcf-refe. The real answer is "copy and paste". That's unfortunately the general solution Microsoft had in mind. In some cases, the .NET 2.0 Settings mechanism can be used, as it bakes the default values into the DLL itself. At runtime, the DLL can then save updated settings - into the .exe.config.

John Saunders
I don't need to have the DLL save settings - they're read-only. The issue is that I can't get the DLLs to load their settings from the merged process.exe.config file. The minute I merge the config files I start getting TypeInitializer exceptions.
BenAlabaster
You should edit your post and add an example of the exception you receive (full stack trace, please, and all inner exceptions). Also provide the config snippet that's involved. Does your main application have the assemblies loaded that contain the config section types?
John Saunders