What is the correct way to use app.config for my plugin when using MEF?

934 views Asked by At

So I have my host program that is going to load in my plugins using a MEF DirectoryCatalogue

So currently I have:

Host.exe and Plugin1.dll

Plugin1.dll has a config setting set in its own config (Plugin1.dll.Config):

<applicationSettings>
        <Plugin1.Settings>
            <setting name="MyString" serializeAs="String">
                <value>Hello</value>
            </setting>
        </Plugin1.Settings>
</applicationSettings>

Which is accessed in Plugin1 using:

var myString = Settings.Default.MyString;

Now the problem is that this is a .dll config and so changing it has no effect, therefore in that case it is practically hard coded and redundant.

Reading around the internet I found:

You get one app.config file per executable (EXE, not DLL). The executable launches, creates its AppDomain, and then loads MyApp.exe.config.

But by adding the config to my Host.exe this means when ever I add a new plugin I would have to redistribute my Host.exe to my clients with the new config instead of Host.exe just dynamically loading whatever plugins it finds.

What is the correct way to handle this situation?

1

There are 1 answers

1
Dennis On BEST ANSWER

First of all, let's distinguish settings from configuration.
At first look, settings and configuration are very similar, but they are different concepts with different purposes.

For simplicity, you may consider configuration as read-only data for end-user. User must not (and often just can't) change configuration, unless it has administrative privileges. E.g., configuration is required run-time version, probing paths for assembly loading, etc. As a rule, there's nothing to change for the user here.

On the other hand, settings are the data, that user can change to make working with the application more convenient. User interface language, to open or not to open some window at startup, connection strings (if application can retrieve data from different data sources) - all of these things are user settings.

Of course, sometimes it's difficult to define clear boundaries between these concepts. Nevertheless, built-in configuration/settings mechanism assumes, that you have single config-file per executable, but you can define custom settings per plugin.

So, if plugin requires some configuration, then yes, you must add this configuration to the config-file (e.g., by creating new configuration section), and redistribute changed config file (you don't need redistribute executable in this case).

But, if plugin requires some settings (applicationSettings suggests an idea, that this is your case), then it is enough to define custom settings type (e.g., add settings in plugin's project). Then, at run-time, you just have to save changes settings - framework will do the rest: it will find settings file, and will update it. In this case, you don't need to update and redistribute config file.

Note, that settings file and config file are different files; settings file is located in the place, where user can change it (%UserProfile%\AppData*).