1.实现方式
//WebApi需要实现的接口 System.Web.Http.Dependencies.IDependencyResolver //MVC需要实现的接口 System.Web.Mvc.IDependencyResolver
2.实现代码
我们写一个通用的类,同时实现这两个接口
public class MefDependencySolver : System.Web.Http.Dependencies.IDependencyResolver, System.Web.Mvc.IDependencyResolver { //实现接口方法 }
3.全局配置
var catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath); var resolver = new MefDependencyResolver(catalog); // Install MEF dependency resolver for MVC DependencyResolver.SetResolver(resolver); // Install MEF dependency resolver for Web API System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = resolver;
参考地址: