我正在尝试使用 MEF 创建 ASP.NET MVC 模块。虽然到目前为止我在不使用 MVC 的情况下使用 MEF 没有任何问题,但在导出控制器时我遇到了一些困难。
我用这种方法作为例子http://kennytordeur.blogspot.de/2012/08/mef-in-aspnet-mvc-4-and-webapi.html http://kennytordeur.blogspot.de/2012/08/mef-in-aspnet-mvc-4-and-webapi.html我通过引入一个包含控制器的外部 dll 使其变得更加复杂。但如果我遵循 Kenny 的想法,那么我需要有一个通用接口(如他的示例中的 IMyTest),但是,由于我计划有多个控制器,这意味着我将需要太多接口。最后,看起来我重用了控制器的内部方法,而不是整个控制器。
我在这里发现了一个问题如何将 MEF 与 ASP.NET MVC 4 和 ASP.NET Web API 集成 https://stackoverflow.com/questions/13566688/how-to-integrate-mef-with-asp-net-mvc-4-and-asp-net-web-api,其中包含一些代码示例,我在其中看到类似的图片 - 接口 IContactRepository 的 _contactRepository 被导入,然后在视图中重用。
这就是为什么我的问题是,在不使用接口的情况下导出整个控制器是否正常?以及如何做到这一点?
我发现 MEF 和 ASP.NET 之间的联系很混乱,一开始似乎网上有很多例子,但当我深入研究时,大多数都是过时的、不实用的或太原始的,看不出它是如何实现的。应用于更大的项目。
Thanks!
我用过MefContrib http://mefcontrib.codeplex.com/与 MVC 4 中的 MEF 一起使用,并使用以下内容来连接所有内容。您需要从Global.asax 中的Application_Start 调用Configure 方法,以便MVC 需要处理请求时一切准备就绪。
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MefContrib.Hosting.Conventions;
using MefContrib.Web.Mvc;
[assembly: PreApplicationStartMethod( typeof(Core.Web.Initialization.DependencyInjection.RegisterDependencyResolver), "RegisterHttpModule" )]
namespace Core.Web.Initialization.DependencyInjection
{
public class RegisterDependencyResolver
{
public static void RegisterHttpModule()
{
// Register the CompositionContainerLifetimeHttpModule HttpModule.
// This makes sure everything is cleaned up correctly after each request.
CompositionContainerLifetimeHttpModule.Register();
}
public void Configure()
{
// Create MEF catalog based on the contents of ~/bin.
//
// Note that any class in the referenced assemblies implementing in "IController"
// is automatically exported to MEF. There is no need for explicit [Export] attributes
// on ASP.NET MVC controllers. When implementing multiple constructors ensure that
// there is one constructor marked with the [ImportingConstructor] attribute.
var catalog = new AggregateCatalog(
new DirectoryCatalog( "bin" ),
new ConventionCatalog( new MvcApplicationRegistry() ) );
// Tell MVC3 to use MEF as its dependency resolver.
var dependencyResolver = new CompositionDependencyResolver( catalog );
DependencyResolver.SetResolver( dependencyResolver );
// Tell MVC3 to resolve dependencies in controllers
ControllerBuilder.Current.SetControllerFactory( new DefaultControllerFactory( new CompositionControllerActivator( dependencyResolver ) ) );
// Tell MVC3 to resolve dependencies in filters
FilterProviders.Providers.Remove( FilterProviders.Providers.Single( f => f is FilterAttributeFilterProvider ) );
FilterProviders.Providers.Add( new CompositionFilterAttributeFilterProvider( dependencyResolver ) );
// Tell MVC3 to resolve dependencies in model validators
ModelValidatorProviders.Providers.Remove( ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single() );
ModelValidatorProviders.Providers.Add( new CompositionDataAnnotationsModelValidatorProvider( dependencyResolver ) );
// Tell MVC3 to resolve model binders through MEF. Model binders must be decorated with [ModelBinderExport].
ModelBinderProviders.BinderProviders.Add( new CompositionModelBinderProvider( dependencyResolver ) );
}
}
}
此外,您需要将控制器导出到 MVC。这是我如何做到这一点的示例:
[Export]
[PartCreationPolicy( CreationPolicy.NonShared )]
public partial class HomeController : ControllerCore
{
[ImportingConstructor]
public HomeController( DataContext context, ILogFactory logFactory, ServiceFactory serviceFactory ) : base( context, logFactory, serviceFactory )
{
}
public virtual ActionResult Index()
{
return View();
}
}
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)