views:

615

answers:

2

Hello,

I am developing a .NET 3.5 Windows Forms app. I have two projects, the UI and a Library.

UI uses strongly typed settings that are stored, as usually, in the app.config file. I read them using UI.Properties.Settings class (generated by Visual Studio).

Library uses its own strongly typed Settings (the Settings.settings file that is dumped into Library.config file).

At runtime, Library settings will not reload from the Library.config file. Only UI.config file is read by the runtime. So I am stuck with default settings in Library assembly, and cannot provide values after deployment.

To summarize: for an application assembly that is not the main executable, the settings are not read at assembly load time.

I know I can use ConfigurationManager.AppSettings["value"] and this will read from the default app config file (UI.config) but what can I do if I want strongly typed settings (read with Properties.Settings class)?

Calling Library.Properties.Settings.Default.Reload() won't do it.

Thanks.

+3  A: 

You can only have one .config file per AppDomain. For a Windows Forms application, that .config file is the file that is named after the executable - that is: UI.exe.config in your case.

This means that while you may have the Library.dll.config file present, the configuration system is never going to pick it up.

You should merge the library's configuration settings into the application configuration file. Configuration files for libraries are not supported by the .NET framework.

Even better, redesign your library so that it does not rely on configuration files, but rather uses Imperative Configuration.

Mark Seemann
Thanks, I'll check your post.One config file leaves me with the problem of not being able to acces settings in Library using my strongly typed class.
Vasi
@Vasi: As I mentioned above, and as Pop Catalin demonstrates in his answer, you can merge your library settings into the application config file.
Mark Seemann
+1  A: 

What you need to do is merge your library config sections to app.connfig. Merging config files is done by first adding elements inside the <configSections> config element, to identity the config section and then by adding the config elements inside the configuration element.

Example of merging config files:

App config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="CA3.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <CA3.Settings>
            <setting name="Setting" serializeAs="String">
                <value>2</value>
            </setting>
        </CA3.Settings>
    </userSettings>
</configuration>

Library config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="CA3.Library" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <CA3.Library>
            <setting name="Setting" serializeAs="String">
                <value>1</value>
            </setting>
        </CA3.Library>
    </userSettings>
</configuration>

Merged app.config containing both library and app configuration.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="CA3.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            <section name="CA3.Library" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <CA3.Settings>
            <setting name="Setting" serializeAs="String">
                <value>2</value>
            </setting>
        </CA3.Settings>
        <CA3.Library>
            <setting name="Setting" serializeAs="String">
                <value>1</value>
            </setting>
        </CA3.Library>
    </userSettings>
</configuration>
Pop Catalin
Yep, thanks, I have tried the same solution just now with my test solution and it works.It's not straioghtforward, I have to manually merge the config files, but I can use the generated class to access the settings, which is good. I really don't like using strings like ConfigurationManager.AppSettings["Color"].
Vasi
Well, your answer came earlier than mine, so I deleted mine.Thanks again!
Vasi