0

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

我做了什么:

  • 我使用空模板和 MVC 引用创建了一个 ASP.NET MVC 项目。
  • 我添加了Bootstrapper,CustomControllerFactoryCustomViewEngineclasses 以及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属性中,我可以看到问题与程序集版本有关(请参见此处)。

  • dependentAssembly通过将声明从模块复制web.configWebHost配置文件,我找到了“解决”这个问题的解决方法。但是我想避免这种情况,因为WebHost不应该根据模块的需要进行修改。
  • 即使在解决方法之后,它也不起作用。在渲染视图时,它抛出了这个异常:

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

4

1 回答 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()
        {
        }
    }
于 2020-03-10T15:16:09.770 回答