MEF parts configuration, where to store?

2020-07-13 09:42发布

问题:

In ASP.NET, .NET 4.0, MEF, I put all parts in a folder and importing them using DirectoryCatalog. Everything is fine. Some of part have related configurations. I don't want put them in web.config. Maybe a good approach be a config file just beside the part with a .config added to the end.

What is the best way for storing part configuration ?

回答1:

I was able to create a separate config file for each DLL and successfully was able to read the keys. But when I tried to create a custom config section, I was not able to read from the DLL's app config. It always wanted to read from the MEF Consumer exe.

Here is the code to read from the consumed dll's own app config

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location); 
    return appConfig.AppSettings.Settings["SomeKeyName"].Value;  

Since I was not able to read from a custom config section, I ended up using xml and serializing the custom configuration section to an object. Since you are just getting the mail server info you will have no problem using app.config.