加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – MVC4 MEF插件和控制器命名空间

发布时间:2020-12-16 04:27:25 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试使用许多插件创建一个MVC4 Web应用程序,即基本上通过MEF导出的控制器以及解压缩到正确位置的内容文件.我找到了很多关于MVC插件的资料,主要涉及领域,但我不得不放弃MvcContrib,这是最明显的解决方案,因为它似乎没有更多的开发,显示了最新的MVC位的
我正在尝试使用许多插件创建一个MVC4 Web应用程序,即基本上通过MEF导出的控制器以及解压缩到正确位置的内容文件.我找到了很多关于MVC插件的资料,主要涉及领域,但我不得不放弃MvcContrib,这是最明显的解决方案,因为它似乎没有更多的开发,显示了最新的MVC位的一些问题,而且我也喜欢这种架构的最简单复杂的实现.

因此我的要求是:

a)一个基于MEF的MVC插件解决方案,我只是在我的站点中删除一个包以供它使用,理想情况下甚至没有重启.这意味着将插件存储在与Bin不同的文件夹中,这也提供了更好的隔离.

b)符合IoC工具的解决方案比仅由MEF完成的解决方案更完整.我倾向于使用Autofac,因为它与MEF和MVC4(此时为RC)集成.

除了像视图这样的内容之外,其基本任务是让MVC在MEF插件中定位控制器并实例化它们,所以我需要一个控制器工厂.我在这里发现了一篇很好的文章:http://kennytordeur.blogspot.be/2012/08/mef-in-aspnet-mvc-4-and-webapi.html(我联系了Kenny,我感谢他指点我的路由问题).作者还将他的代码包装成一个方便的nuget包(MEF.MVC4).无论如何,我发现了一个似乎与路由和命名空间有关的问题:当遇到一个到插件控制器的路由时,MEF控制器工厂的GetControllerInstance方法得到一个null controllerType,最终导致404.我想我可能找到了罪魁祸首通过阅读这些帖子:

http://blog.davebouwman.com/2011/12/08/asp-net-mvc3-and-404s-for-area-controllers/

Custom Controller Factory,Dependency Injection / Structuremap problems with ASP.NET MVC

我想(但我可能错了)问题出在路由约定和插件区域控制器命名空间中:插件控制器的命名空间不在主机Web的同一“根”中.该帖子中提出的解决方案只是为Web应用程序添加了一条新路径,但这并不适用于区域作为插件工作的动态添加到主机应用程序的解决方案.我的主机网络应用程序必须保持不知道插件,这当然应该是一个相当普遍的要求,但我找不到明显的解决方案.

Repro解决方案

您可以快速创建repro解决方案,以便按照以下步骤查看我的方法的详细信息,或从here下载:

1)创建一个空白解决方案.

2)创建一个MVC4 Web应用程序(HostWeb),更新所有NuGet预装的包并添加Mef.MVC4,Autofac MVC 4(RC)和Autofac.Mef.在我的真实应用程序中,我想使用Autofac在构造函数中注入控制器的依赖项.

3)在HostWeb中创建一个Plugins文件夹,并在其中创建一个子文件夹Temp.这将包括使用Temp子文件夹作为卷影副本容器的插件,以便从它而不是直接从插件加载MEF目录.这与一些启动代码一起应该让我更新插件而不必重新启动Web应用程序(否则将锁定DLL).启动代码是一个名为PreApplicationInit的类,可以在Infrastructure文件夹中找到(稍微修改自http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx).

4)将一个部件区域添加到主机Web并从视图根文件夹中复制_ViewStart文件(并更改布局视图中的现有链接,以便将空白区域添加到路径值,以便它们不会被破坏) .所有插件控制器都将命名空间到名为Parts的区域中.主机Web应用程序有一个没有控制器的区域,只是为插件内容文件准备文件夹结构和路由(视图,将从插件安装程序模块解压缩到正确的位置,而二进制文件将放在插件中).

5)在HostWeb的App_Start中定制MefConfig并添加处理Autofac的IocConfig.然后向全局asax添加对两者的调用:MefConfig.RegisterMef()和IocConfig.RegisterDependencies().

6)在其中创建另一个MVC4 Web应用程序(AlphaPlugin),更新所有NuGet预安装的软件包并添加Autofac MVC 4(RC)和Autofac.Mef.我选择了一个Web应用程序模板(而不是类库),因此我可以使用MVC的所有VS工具,并最终直接在那里进行一些测试.

7)将一个部件区域添加到主机Web,并从views根文件夹中将_ViewStart文件复制到其中.

8)在“部件”区域中添加可导出的控制器.我的名字叫AlphaController,它只有一个名为Hail的动作方法,它在ViewBag中输入一个字符串并返回默认视图.

9)回到主机,只需在主视图中添加插件控制器动作的链接,即可通过MEF进行测试.

现在,如果我构建所有并将AlphaPlugin.dll二进制文件复制到HostWeb Plugins文件夹中,我希望MVC通过MEF找到它,但随后抛出一个未找到视图的错误,因为我还没有复制主机网络中的任何内容文件.相反,我得到以下内容:

Value cannot be null.
Parameter name: type
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: type

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:
[ArgumentNullException: Value cannot be null.
Parameter name: type]
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type,Type metadataViewType,String contractName,ImportCardinality cardinality) +263923
   System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(Type type,String contractName) +41
   MEF.MVC4.MefControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType) +84
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName) +226
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController& controller,IControllerFactory& factory) +326
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback callback,Object state) +177
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously) +155

这是最相关的代码(您可以在repro解决方案中找到它):这将处理Plugins文件夹内容,以便Web应用程序从副本加载它们:

[assembly: PreApplicationStartMethod(typeof(PreApplicationInit),"Initialize")]
static public class PreApplicationInit
{
    /// 
    /// The source plugin folder from which to shadow copy from.
    /// 
    /// This folder can contain sub folders to organize plugin types.
    internal static DirectoryInfo PluginFolder { get; private set; }

    /// 
    /// The folder to shadow copy the plugin DLLs to use for running the app.
    /// 
    internal static DirectoryInfo ShadowCopyFolder { get; private set; }

    static PreApplicationInit()
    {
        PluginFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/Plugins"));
        ShadowCopyFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/Plugins/Temp"));
    }

    public static void Initialize()
    {
        if (!Directory.Exists(ShadowCopyFolder.FullName))
            Directory.CreateDirectory(ShadowCopyFolder.FullName);
        else
        {
            foreach (FileInfo fi in ShadowCopyFolder.GetFiles("*.dll",SearchOption.AllDirectories))
            {
                try
                {
                    fi.Delete();
                }
                catch (Exception ex)
                {
                    // TODO log
                    Debug.WriteLine(ex.ToString());
                }
            }
        }

        // shadow copy files
        foreach (FileInfo fi in PluginFolder.GetFiles("*.dll"))
        {
            try
            {
                File.Copy(fi.FullName,Path.Combine(ShadowCopyFolder.FullName,fi.Name),true);
            }
            catch (Exception ex)
            {
                // TODO log
                Debug.WriteLine(ex.ToString());
            }
        }
    }
}

这是我的工厂,无论如何都得到一个null controllerType,所以它的代码永远不会超出它的第一行:

public class MefControllerFactory : DefaultControllerFactory
{
    private readonly CompositionContainer _compositionContainer;

    public MefControllerFactory(CompositionContainer compositionContainer)
    {
        _compositionContainer = compositionContainer;
    }

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext,Type controllerType)
    {
        // https://stackoverflow.com/questions/719678/custom-controller-factory-dependency-injection-structuremap-problems-with-asp
        if (controllerType == null) return base.GetControllerInstance(requestContext,null);

        var export = _compositionContainer.GetExports(controllerType,null,null).SingleOrDefault();

        IController result;

        if (export != null) result = export.Value as IController;
        else
        {
            result = base.GetControllerInstance(requestContext,controllerType);
            _compositionContainer.ComposeParts(result);
        }

        return result;
    }

解决方法

控制器工厂需要找到适合外部MEF组件的正确控制器类型.像这样覆盖MefControllerFactory类的GetControllerType方法.
public class MefControllerFactory : DefaultControllerFactory
{
    protected override Type GetControllerType(RequestContext requestContext,string controllerName)
    {
        var controllerType = base.GetControllerType(requestContext,controllerName);

        if (controllerType == null)
        {
            var controller = _compositionContainer.GetExports<IController,IControllerMetaData>().SingleOrDefault(x => x.Metadata.ControllerName == controllerName).Value;

            if (controller != null)
            {
                return controller.GetType();
            }
       }
       return controllerType;
    }
}

其中IControllerMetaData是指定控制器名称的接口

public interface IControllerMetaData
{
    string ControllerName { get;}
}

并且您的控制器在元数据中指定controllerName.例如.

[Export (typeof(IController))]
[ExportMetadata("ControllerName","Home")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class HomeController : Controller,IController
{
    public ActionResult Index()
    {
        return new EmptyResult();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读