项目必须选为 :Framework 4.5.2
引入 autofac,autofac.mvc5
var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); //注入Controller,解决必须写无参构造函数的问题 //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces(); //builder.RegisterType<Consul>().As<IConsult>(); builder.RegisterType<Consult>().AsImplementedInterfaces(); 注入Consult类,以接口的方式注入 //builder.RegisterInstance(new Consult()).As<IConsult>(); //SetupResolveRules(builder); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 注入容器参考文章:
http://blog.csdn.net/zunguitiancheng/article/details/50681374 自动注入
http://www.tuicool.com/articles/IBR77zz
http://www.cnblogs.com/ancupofcoffee/p/5014607.html autofac注入方式总结
http://www.cnblogs.com/jiagoushi/p/4084145.html 文章不好,但是里面有Module注册的例子,可以稍微借鉴
http://www.cnblogs.com/n-pei/archive/2013/01/24/2875674.html 自动注入模式,,之前项目中有这样写过,保留一份,留着看看
http://www.cnblogs.com/jys509/p/4649798.html 注入方法的说明