Assembly specific settings not loading at runtime

好久不见. 提交于 2021-02-19 02:43:05

问题


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.


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/1399299/assembly-specific-settings-not-loading-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!