Search code examples
.net.net-coreconfigurationconfiguration-filesconfigurationmanager

Can a NetCore application config overwrite values from class library using Properties.Settings?


In NetFramework, you could have a class library with a Settings.Setting file which would ultimately feed a app.config file like this:

    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="Namespace.Lib.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Namespace.Lib.Properties.Settings>
            <setting name="MyVariable" serializeAs="String">
                <value>Default Value</value>
            </setting>
        </NameSpace.Lib.Properties.Settings>
    </applicationSettings>

and inside the class library code you would read the value using Properties.Settings.Default.MyVariable

After that, in your main application, you could also have a app.config (or web.config) with the exact same section as above, but with a different value for the variable, and it would overwrite the default value so the lib would use what your application require.

--

Now in NetCore, I'm aware the new method is using the appsettings.json file, and the host Startup receive an IConfiguration class where it can read the settings, feed models, and even get injected to services. But for that to work, your class library needs to be coded in a way to accept the new configuration format, or a model.

I also know that the System.Configuration.ConfigurationManager NuGet package was introduced to keep supporting old config file format. And using it in the class library allow it to use its app.config value as before without any error.

--

I have a class library using the Properties.Settings.Default in its code that I don't want to recode to accept IConfiguration instead (to keep compatibility with other NetFramework projects still using it). I ported the class library to netstandard2.0 and added the configuration NuGet package and it build.

My problem is, from my NetCore application, even if I add the NuGet package, and add a app.config (or web.config) manually and overwrite the sections as before, it doesn't seems to get propagated to the class library or be used at all. When the class library access Properties.Settings.Default.MyVariable it still read its own default value and not the one I'm trying to overwrite from my application. Why is that?

It is still possible to do that?


Solution

  • Problem was only file names.

    Using "Web.config" allow IIS to read server the <system.webServer> sections. But to be used in the application, it needs to be named "ApplicationName.dll.config".

    Previously ASP.NET from NetFramework would automatically copy the web.config to the application name, now seems like it doesn't.