ASP.NET MVC & MEF - Pluggable architecture

人走茶凉 提交于 2020-03-26 03:49:08

问题


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

What I did:

  • 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.

What didn't work:

  • 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. In FusionLog property I can see that the problem is related to the assembly version (see here).

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


回答1:


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

You need something like--

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()
        {
        }
    }


来源:https://stackoverflow.com/questions/60619707/asp-net-mvc-mef-pluggable-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!