安装AutoFac
在Nuget中引入AutoFac、Autofac.Extensions.DependencyInjection(Autofac的扩展)
配置Program.cs
//实例化AutoFac工厂实例,替换默认容器工厂
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
配置Startup.cs
public void ConfigureContainer(ContainerBuilder builder)
{
//告诉autofac框架注册数据仓储层所在程序集中的所有类的对象实例
Assembly iRepository = Assembly.Load("Webapi.Core.IRepository");
//告诉autofac框架注册业务逻辑层所在程序集中的所有类的对象实例
Assembly repository = Assembly.Load("Webapi.Core.Repository");
//以接口形式保存被创建类的对象实例
builder.RegisterAssemblyTypes(iRepository, repository)
.Where(x => x.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
}