MEF部件的配置,在哪里存储?部件、MEF

2023-09-07 14:34:50 作者:寻栖。

ASP.NET .NET 4.0 MEF ,我把所有的部分文件夹中,并使用 DirectoryCatalog 导入它们。一切安好。其中一些部分有相关的配置。我不想把它们放在的web.config 。也许一个好的方法是配置文件只是旁边的部分的.config 添加到末尾。

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.

什么是存储部分配置的最佳方法是什么?

推荐答案

我能够为每个DLL一个单独的配置文件,并成功能够读取钥匙。但是,当我试图创建一个自定义的配置部分,我是不是能够从DLL的程序config读取。它一直想从MEF消费者exe文件阅读。

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.

下面是code从消费dll的自己的应用程序config读取

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;  

由于我无法从自定义配置节阅读,我结束了使用XML和序列化的自定义配置部分,以一个对象。既然你是刚刚起步的邮件服务器信息,您将使用的app.config没有问题。

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.