WebAPi依赖注入 使用记录 笔记
1.NuGet包安装
2.控制器加入构造函数
3.Global.asax ----Application_Start
应用程序启动时
using Autofac;
using Autofac.Integration.WebApi;
/*依赖注入*/ var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<DbService>(); var container = builder.Build(); var resolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = resolver;