三层架构对web api的依赖注入
需引用
using Autofac.Integration.WebApi;
Autofac 配置:
public static void Register()
{
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetCallingAssembly())//注册mvc的Controller
.PropertiesAutowired();//属性注入
//2、有接口类注入
//注入BLL,UI中使用
builder.RegisterAssemblyTypes(Assembly.Load("BLL"))
.InstancePerRequest()
.PropertiesAutowired(); //属性注入
//注入DAL,BLL层中使用
builder.RegisterAssemblyTypes(Assembly.Load("DAL"))
.InstancePerRequest()
.PropertiesAutowired(); //属性注入
var container = builder.Build();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
在Global引用
AutoFacConfig.Register();