ASP.NET MVC和MEF-可插拔架构 [英] ASP.NET MVC & MEF - Pluggable architecture

查看:103
本文介绍了ASP.NET MVC和MEF-可插拔架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这篇文章开始创建PoC,但是我无法获得一个非常基本的示例来工作

I started creating a PoC following SO this post but I wasn't able to get a very basic sample to work.

我做了什么:

  • 我使用空模板和MVC引用创建了一个ASP.NET MVC项目.
  • 我添加了BootstrapperCustomControllerFactoryCustomViewEngine类以及Application_Start中的相应行.
  • 我使用MVC模板创建了一个ASP.NET MVC项目.
  • 我在HomeController上添加了ExportPartCreationPolicy装饰器.
  • 我将模块项目发布到/Modules目录中的某个文件夹中-在WebHost根路径中.
  • I created an ASP.NET MVC project using empty template and MVC references.
  • I added Bootstrapper, CustomControllerFactory and CustomViewEngine classes and also the corresponding lines in Application_Start.
  • I created an ASP.NET MVC project using MVC template.
  • I added the Export and PartCreationPolicy decorators on HomeController.
  • I published the module project to a folder inside /Modules directory -in WebHost root path.

什么不起作用:

  • 方法CompositionContainer.GetExportedValue引发异常,表明它无法加载一个或多个必需的类型,并且有关LoaderExceptions属性的更多信息.该属性是一个数组,其中包含77个实例,这些实例似乎是相同的例外:
  • The method CompositionContainer.GetExportedValue throws an exception saying that it couldn't load one or more required types and that there is more information on LoaderExceptions property. That property is an array with 77 instances of what appears to be the same exception:

Could not load file or assembly Antlr3.Runtime, Version=3.4.1.9004, Culture=neutral, PublicKeyToken=eb42632606e9261f or one of its dependencies.FusionLog属性中,我可以看到问题与程序集版本有关(请参阅此处).

Could not load file or assembly Antlr3.Runtime, Version=3.4.1.9004, Culture=neutral, PublicKeyToken=eb42632606e9261f or one of its dependencies. In FusionLog property I can see that the problem is related to the assembly version (see here).

  • 我发现了一种解决方法,可以通过将dependentAssembly声明从模块的web.config复制到WebHost配置文件中来解决".但是,我想避免这种情况,因为WebHost不应根据模块的需要进行修改.
  • 即使在解决方法之后,它也没有起作用.在渲染视图时,它引发了以下异常:

  • I found a workaround for "solving" this by copying the dependentAssembly declarations from the module's web.config to the WebHost configuration file. However I'd like to avoid this as the WebHost should not be modified based on the module's needs.
  • Even after the workaround, it didn't work. While rendering the view, it threw this exception:

    CS0234: The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

  • 推荐答案

    为您提供帮助,请使用MEF共享一个完整的测试项目.请访问此 github 链接.

    For your help is share a full test project using MEF. visit this github link.

    您需要-

    public class AzRDependencyResolver : System.Web.Http.Dependencies.IDependencyResolver, System.Web.Mvc.IDependencyResolver
        {
            private readonly CompositionContainer _container;
    
            public AzRDependencyResolver(CompositionContainer container)
            {
                _container = container;
            }
    
            public IDependencyScope BeginScope()
            {
                return this;
            }
    
            /// <summary>
            /// Called to request a service implementation.
            /// 
            /// Here we call upon MEF to instantiate implementations of dependencies.
            /// </summary>
            /// <param name="serviceType">Type of service requested.</param>
            /// <returns>Service implementation or null.</returns>
            public object GetService(Type serviceType)
            {
                if (serviceType == null)
                    throw new ArgumentNullException("serviceType");
    
                var name = AttributedModelServices.GetContractName(serviceType);
                var export = _container.GetExportedValueOrDefault<object>(name);
                return export;
            }
    
            /// <summary>
            /// Called to request service implementations.
            /// 
            /// Here we call upon MEF to instantiate implementations of dependencies.
            /// </summary>
            /// <param name="serviceType">Type of service requested.</param>
            /// <returns>Service implementations.</returns>
            public IEnumerable<object> GetServices(Type serviceType)
            {
                if (serviceType == null)
                    throw new ArgumentNullException("serviceType");
    
                var exports = _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
                return exports;
            }
    
            public void Dispose()
            {
            }
        }
    

    这篇关于ASP.NET MVC和MEF-可插拔架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆