更新在运行时MEF目录

问题描述:

我目前正在与MEF MVC的Web应用程序,这样一)开发者可以为网站的插件,用户可以选择他们想要在他们的帐户,这插件。这意味着,我的一些成分具有后的应用程序已经启动,用户登录的情况发生(都到数据库中,抓住组件在塞并将它们添加到当前目录)。

I am currently working on an MVC web app with MEF so that a) developers can develop plugins for the website, and users can elect which plugins they want on their account. This means that some of my composition has to happen after the app has already started and the user has logged in (which goes to the database, grabs the assemblies for the plug in and adds them to the current catalog).

它的工作方式是我有一对夫妇,我存储在数据库中,并拉出当用户登录到加载到 AssemblyCatalog ,我可以图书馆成功在我的作曲者添加到现有 AggregateCatalog 。然而,在我的图书馆之一我发现没有出口,导致该特定窗口部件,打破了一个IController 合同。我不是如何强制MEF启动后重新构图完全肯定。我在这里发帖,希望有人对我一些建议。

The way it works is I have a couple of libraries that I store in the database and pull out when the user logs in to load into an AssemblyCatalog, which I can successfully add to my existing AggregateCatalog in my Composer. However, in one of my libraries I have an IController contract that isn't getting exported, causing that specific widget to break. I'm not entirely sure on how to force MEF to recompose after startup. I'm posting here, hoping that someone has some advice for me.

我的code基于关闭href=\"http://www.fidelitydesign.net/?p=104\"由马特·雅培 rel=\"nofollow\">优秀的博客系列

My code is based off the excellent blog series by Matt Abbott, so I'm kind of hoping that he'll chime in with some advice as well.

好了,你不能只加载所有可用的零部件(无论哪个客户有他们启用),但使用延迟的实例化,无论是通过 ExportFactory 与元数据的组合来确定部分客户端系统?你不会需要按需加载组件那样的话,他们就在那里准备好了。

Well, couldn't you just load ALL available parts (regardless of which clients have them enabled), but use deferred instantiation, either through Lazy or ExportFactory with a combination of metadata to identify the parts to your client system? That way you wouldn't need to load assemblies on demand, they are just there ready to go.

如果它是你动态地希望的情况下读取数据库的组件,并将其加载到你的的AppDomain ,你也许可以把它包装起来的一个新的 AssemblyCatalog ,而是采取重排的优势。

If its the case that you want dynamically read an assembly from your database and load it into your AppDomain, you can probably wrap it up in a new AssemblyCatalog, but take advantage of recomposition.

例如,在早期版本中,我使用 PartCreator< T,TMetadata> 这演变成 ExportFactory< T,TMetadata> 即将发行(但不包含在最终的.NET 4.0)。如果你改变了 [ImportMany] [ImportMany(AllowRecomposition = TRUE)] ,下一次你添加一个新的目录,例如:

E.g., in that early version, I was using PartCreator<T,TMetadata> which evolved into ExportFactory<T,TMetadata> nearing release (but not included in final .NET 4.0). If you change the [ImportMany] to [ImportMany(AllowRecomposition = true)], next time you add a new catalog, e.g.:

((AggregateCatalog)container.Catalog).Catalogs.Add(<assembly>);

这应该重新选 [ImportMany] PartCreator ExportFactory $的C $ C>)的实例。

不知道是否会工作......我需要了解你的意图的架构之前,我可以说,它会或不会工作...

Not sure if that would work... I'd need to understand your intended architecture before I can say it would or would not work...