public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ContainerBuilder builder = new ContainerBuilder(); //实现控制器属性注入的关键--PropertiesAutowired() builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired(); //实现过滤器属性注入的关键 builder.RegisterFilterProvider(); //注册类库程序集 builder.RegisterAssemblyTypes(Assembly.Load("Repository")).AsImplementedInterfaces().PropertiesAutowired(); //注册类库程序集 builder.RegisterAssemblyTypes(Assembly.Load("Services")).AsImplementedInterfaces().PropertiesAutowired(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } }