ASP.NET MVC2 和 MEF - 为什么我的 MefControllerFactory 无法获取导出或元数据?

发布于 2024-10-17 21:40:26 字数 5098 浏览 2 评论 0 原文

我正在关注这篇博客文章: 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?)   

我很困惑为什么它不能识别 ExportMetadata。你们有什么想法吗?

编辑

我将这一行更改

 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?)

I am following this blog post: http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx and I'm having dificulty implementing the MefControllerFactory.

MefControllerFactory Code

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
    }
}

The errors I'm getting are:

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?)   

I'm rather confused as to why this wouldn't recognize Export or Metadata. Do you guys have any thoughts?

Edit

I changed the line:

 Export<IController> export = this.container.GetExports<IController>()
                                                 .Where(c => c.Metadata.ContainsKey("controllerName")
                                                     && c.Metadata["controllerName"].ToString() == controllerName)
                                                 .FirstOrDefault();

To:

var export = this.container.GetExports<IController>()
                                                 .Where(c => c.Metadata.ContainsKey("controllerName")
                                                     && c.Metadata["controllerName"].ToString() == controllerName)
                                                 .FirstOrDefault();

That took care of my issues with Metadata. But now i hev a new error with the next if statement:

            if (export != null)
            {
                controller = export.GetExportedObject(); 
            }

error:

 '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风起 2024-10-24 21:40:27

该博客文章基于 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 of export.GetExportedObject().

送你一个梦 2024-10-24 21:40:27

您也可以使用:

Lazy<IController> export = this.container.GetExports<IController, IDictionary<string, object>>()
    .Where(c => c.Metadata.ContainsKey("ControllerName")
        && c.Metadata["ControllerName"].ToString().ToLowerInvariant().Equals(controllerName.ToLowerInvariant())).
            FirstOrDefault();

此外,您需要实现 IControllerFactory 函数=>

public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }

新导出

icontrollerfactory-实现

also you can use :

Lazy<IController> export = this.container.GetExports<IController, IDictionary<string, object>>()
    .Where(c => c.Metadata.ContainsKey("ControllerName")
        && c.Metadata["ControllerName"].ToString().ToLowerInvariant().Equals(controllerName.ToLowerInvariant())).
            FirstOrDefault();

also , you need to implements the IControllerFactory function = >

public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }

new export

icontrollerfactory-implementation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文