ASP.NET MVC2 和 MEF - 为什么我的 MefControllerFactory 无法获取导出或元数据?
我正在关注这篇博客文章: http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx 和我在实现 MefControllerFactory
时遇到困难。
MefControllerFactory 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Web.Mvc;
namespace plugme.Utilities
{
public class MefControllerFactory : IControllerFactory
{
private string pluginPath;
private DirectoryCatalog catalog;
private CompositionContainer container;
private DefaultControllerFactory defaultControllerFactory;
public MefControllerFactory(string pluginPath)
{
this.pluginPath = pluginPath;
this.catalog = new DirectoryCatalog(pluginPath);
this.container = new CompositionContainer(catalog);
this.defaultControllerFactory = new DefaultControllerFactory();
}
#region IControllerFactory Members
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
IController controller = null;
if (controllerName != null)
{
string controllerClassName = controllerName + "Controller";
// "Export" isn't recognized
// and "Metadata" (as in c => c.Metadata ) isn't recognized.
Export<IController> export = this.container.GetExports<IController>()
.Where(c => c.Metadata.ContainsKey("controllerName")
&& c.Metadata["controllerName"].ToString() == controllerName)
.FirstOrDefault();
if (export != null)
{
controller = export.GetExportedObject();
}
}
if (controller == null)
{
return this.defaultControllerFactory.CreateController(requestContext, controllerName);
}
return controller;
}
public void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
#endregion
}
}
我收到的错误是:
Error 1 The type or namespace name 'Export' could not be found
(are you missing a using directive or an assembly reference?)
Error 2 'System.Lazy<System.Web.Mvc.IController>' does not contain a
definition for 'Metadata' and no extension method 'Metadata'
accepting a first argument of type
'System.Lazy<System.Web.Mvc.IController>' could be found
(are you missing a using directive or an assembly reference?)
Error 3 'System.Lazy<System.Web.Mvc.IController>' does not contain a
definition for 'Metadata' and no extension method 'Metadata'
accepting a first argument of type
'System.Lazy<System.Web.Mvc.IController>' could be found
(are you missing a using directive or an assembly reference?)
我很困惑为什么它不能识别 Export
或 Metadata
。你们有什么想法吗?
编辑
我将这一行更改
Export<IController> export = this.container.GetExports<IController>()
.Where(c => c.Metadata.ContainsKey("controllerName")
&& c.Metadata["controllerName"].ToString() == controllerName)
.FirstOrDefault();
为:
var export = this.container.GetExports<IController>()
.Where(c => c.Metadata.ContainsKey("controllerName")
&& c.Metadata["controllerName"].ToString() == controllerName)
.FirstOrDefault();
这解决了我的元数据问题。但现在我在下一个 if 语句中出现了一个新错误:
if (export != null)
{
controller = export.GetExportedObject();
}
错误:
'System.Lazy<System.Web.Mvc.IController,System.Collections.Generic.IDictionary<string,object>>' does not contain a definition for 'GetExportedObject' and no extension method 'GetExportedObject' accepting a first argument of type 'System.Lazy<System.Web.Mvc.IController,System.Collections.Generic.IDictionary<string,object>>' could be found (are you missing a using directive or an assembly reference?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该博客文章基于 MEF 的预览版本。 API 在最终版本之前发生了更改,您应该使用
export.Value
而不是export.GetExportedObject()
。That blog post was based on a preview version of MEF. The API changed before the final release, you should use
export.Value
instead ofexport.GetExportedObject()
.您也可以使用:
此外,您需要实现 IControllerFactory 函数=>
新导出
icontrollerfactory-实现
also you can use :
also , you need to implements the IControllerFactory function = >
new export
icontrollerfactory-implementation