Accessing the configuration file across application domain
Asked Answered
I

1

6

We are implementing a plug and play module for our application where user can load and unload the desired class library at runtime. So I have decided to use MEF and shadow copying of class libraries.

The thing here is each class library may have different configuration properties which needs to set by user. My main application has no knowledge about the configurations present in the class library.

Now the problem is when I try to transfer the application configuration file loaded with class library from one application domain to another.

Without MEF, I have just returned Settings.Default from the class library and I have used it in our main application to edit the settings. With MEF and shadow copying, It doesn't seems to be working because

  1. The object type needs to known to both sides.
  2. I cannot implement MarshalByRefObject on the settings file since the settings file is already extending ApplicationSettingsBase which is an abstract class and c# doesn't supports multiple inheritance.

Currently I am creating a class which holds all the properties as string and creating a GUI in my main application based on this class content.

public class ExtensionModuleConfiguration : MarshalByRefObject
{
    public string Name { get; set; }   
    public string Value { get; set; }
    public List<string> Options { get; set; }
    public UIElements ToolUIElement { get; set; }
}

public enum UIElements
{
    ComboBox,
    TextBox
}

I must say this is not the best solution. Can someone suggest a better way to set the configurations of a class library in MEF?

Identification answered 2/6, 2015 at 7:58 Comment(0)
M
0

There two ways how you can do it. You must inform .NET which app.config should be loaded in the appdomain of your MEF plugin class.

Therefore you can either point particular app.config for your plugin DLL like this:

ConfigurationManager.OpenExeConfiguration("Plugin.dll");
var name = AppSettings.Settings["Name"].Value;

Or you can load the app.config for your main application DLL and put all the appsettings in that file. In this case you should do:

var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
var name = config.AppSettings.Settings["Name"].Value;

Both solutions should be called from within of you Plugin implementation for example in constructor. Or by first call to some lazy loaded configuration property.

Mesquite answered 2/6, 2015 at 8:32 Comment(1)
I want the user to edit the contents of app.config file of the plugin DLL in my main application. I am able to load it with the plugin DLL. What I want is to edit it from main applicationIdentification

© 2022 - 2024 — McMap. All rights reserved.