How to merge the applicationSettings of a class library into the host executable's config file
Asked Answered
T

1

6

I am reading articles, forum posts about applicationSettings for almost a week now.

In almost every thread there was someone that appears to have correctly pointed out that the class libraries cannot have config files when deployed and their applicationSettings configured at design must be specified/merged in the executable.exe.config configuration file of the application that host/consumes the dll.

Not necessarily true.

You can but you don't need to merge them class library settings unless you want to provide the user with a way to "overwrite" the default values - the ones that are specified using the DefaultValueAttribute hard coded in the assembly.

So, for a very simple, practical example. Let's use VB.NET 1. I created a Class Library project called ClassLibrary. 2. Showing all files, expand MyProject, double click Settings.settings. 3. Adding a setting called Message, application scoped whose value is "Hello!". 4. Create a property in Class1.vb (the automatically added class)

Public Class Class1

    Public Shared ReadOnly Property Message As String
        Get
            Return My.Settings.Message
        End Get
    End Property

End Class
  1. Create a VB WinForms project and call it WinForm.
  2. Add a reference to the ClassLibrary project.
  3. Add a button to the already created Form1 and double click on it. 8 Add the some code to the the Button1_Click handler. Should look like this.

    Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        MessageBox.Show(ClassLibrary.Class1.Message)
    End Sub
    

    End Class

  4. Have WinForm "Set as Startup project"

Now, while in the IDE everything works beautifully. Run the solution and you'll get the expected Hello! when you press the button. If you go and change the setting in the app.config of the library to say "Good bye!" and you run the solution again you get a "Good bye!"

However, right click on the WinForm project and "Open in Explorer" and get to the Debug folder. There's no WinForm.exe.config file yet. Let's create one quickly. Switch back to VS and while the WinForm project is selected click to Show All Files. Expand MyProject, open Settings.settings, create a setting (doens't matter what) and save. There we go, an App.config was created and if I build this solution, the Debug folder will contain a WinForm.exe.config.

Tell me how I merge my Message setting from the class library config

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ClassLibrary.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <applicationSettings>
        <ClassLibrary.My.MySettings>
            <setting name="Message" serializeAs="String">
                <value>Hello!</value>
            </setting>
        </ClassLibrary.My.MySettings>
    </applicationSettings>
</configuration>

into the WinForm's config

<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WinForm.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
    </startup>
    <applicationSettings>
        <WinForm.My.MySettings>
            <!--<setting name="A" serializeAs="String">
                <value>A</value>
            </setting>-->
        </WinForm.My.MySettings>
    </applicationSettings>
</configuration>

so I can change the Message setting's value in the WinForm.exe.config to something else and the application will display this new value overriding the DefaultValueAttribute.

Tameshatamez answered 12/4, 2012 at 1:48 Comment(0)
T
3

I have done some research lately for this applicationSettings problem. I found two relatively convenient ways of doing what I was asking for. I put my thoughts about it together and wrote a blog entry here.

  1. assemblies that your main assembly depend on can and will use settings you define in their individual projects but only during development just because they are build on the fly and contain whatever values you set last time. However when you deploy and think that you can place .config for each assembly they won't work if you expect to change a setting's value and have that reflected at runtime, that's because the last values are set as default values and are hardcoded in.
  2. So you have to either move all your settings to the appSettings in the .config file of the main assembly and inject them at runtime as needed to the assemblies needing them (passing them as arguments).
  3. Otherwise let's say these are your config files while in development:

ClassLibrary.dll.config2

Winform.exe.config3

For deployment you will have to create a configuration section in the main config file and point to

Modified Winform.exe.config

your modified "ClassLibrary.dll.config":

enter image description here

You don't have to point to external files though, the config sections declarations can point to their definitions further down in the main config file like this.

internalsections

Tameshatamez answered 17/4, 2012 at 2:50 Comment(2)
The linked blog post has two methods, the second of which worked for me. In the event that the link ever dies, just search for "applicationSettings configSource" to find the relevant information.Atalayah
Could you post your answer here in SO? Links to external blogs can go stale if you ever discontinue your blog. Also, my company's "nanny-bot" won't let me get to your blog.Jamey

© 2022 - 2024 — McMap. All rights reserved.