使用 MEF 在控制器中导入值集合的最佳方法
我有一个支持可视化插件/提供程序的 ASP.NET MVC2 应用程序。 IVisualization 接口在公共程序集中定义,ASP.NET MVC2 应用程序和任何可视化提供程序都引用该程序集。
在可视化控制器中,我有一个方法可以返回给定数据集的所有适用可视化效果。为了扫描可用的提供程序,我在控制器的 ActionMethod 中使用以下代码。
var catalog = new DirectoryCatalog(HttpRuntime.BinDirectory);
var container = new CompositionContainer(catalog);
var visualizations = container.GetExportedValues<IVisualization>();
但是,我觉得如果控制器中有以下内容
[ImportMany]
public IEnumerable<IVisualization> Visualizations { get; set; }
,那么导入应该会自动发生。我错过了什么阻止自动导入?
另外,我当前使用的代码是否会破坏网站的扩展?
谢谢, 埃里克
I have an ASP.NET MVC2 application that supports visualization plug-ins/providers. The IVisualization interface is defined in a common assembly which is referenced by both the ASP.NET MVC2 app, and any visualization providers.
In the Visualization controller, I have a method which returns all the applicable visualizations for a given set of data. In order to scan the available providers, I use the following code in the controller's ActionMethod.
var catalog = new DirectoryCatalog(HttpRuntime.BinDirectory);
var container = new CompositionContainer(catalog);
var visualizations = container.GetExportedValues<IVisualization>();
However, I feel like if I have the following in the controller
[ImportMany]
public IEnumerable<IVisualization> Visualizations { get; set; }
then the import should happen automatically. What am I missing that prevents the automatic imports?
Also, is the code that I am currently using going to kill scaling of website?
Thanks,
Erick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了让 MEF 满足导入,它还需要负责控制器的实例化。您可以在 MVC 中使用自定义控制器工厂来完成此操作。您可以在我的博客中找到此示例(可能已过时): 链接
In order for MEF to satisfy the imports, it needs to also be responsible for the instantiation of the controller. You can do that in MVC by using a custom controller factory. You can find a sample (maybe outdated) of this in my blog: Link
如果您有一个声明特定属性导入的控制器,则必须以编程方式调用 MEF 的方法之一来满足它们。
一些选项包括:
除其他外。
我希望这能说明我的观点。
If you have a controller that declares that particular property import, you must programatically call one of MEF's methods to satisfy them.
Some options are:
among others.
I hope this illustrates my point.