ASP.NET MVC&MEF-可插拔体系结构

问题描述 投票:0回答:1

我在SO this post之后开始创建PoC,但是我无法获得非常基本的示例来工作。

我做了什么:

  • 我使用空模板和MVC引用创建了ASP.NET MVC项目。
  • 我添加了BootstrapperCustomControllerFactoryCustomViewEngine类以及Application_Start中的相应行。
  • 我使用MVC模板创建了一个ASP.NET MVC项目。
  • 我在Export上添加了PartCreationPolicyHomeController装饰器。
  • 我将模块项目发布到/ Modules目录内的文件夹中-WebHost根路径中。

无效的内容:

  • CompositionContainer.GetExportedValue方法引发异常,表明它无法加载一个或多个必需类型,并且有关LoaderExceptions属性的信息更多。该属性是一个数组,其中包含77个实例,这些实例似乎是相同的例外:

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

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

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

asp.net-mvc mef modular-design
1个回答
0
投票

为了帮助您,请使用MEF共享完整的测试项目。访问此github链接。

您需要类似-

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()
        {
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.