安装 autofac
安装 autofac.mvc
注册代码
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
//注册所有实现了 IDependency 接口的类型
Type baseType = typeof(IDependency);
Assembly[] assemblies = Directory.GetFiles(AppDomain.CurrentDomain.RelativeSearchPath, "*.dll").Select(Assembly.LoadFrom).ToArray();
builder.RegisterAssemblyTypes(assemblies)
.Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract)
.AsSelf().AsImplementedInterfaces()
.PropertiesAutowired().InstancePerLifetimeScope();
//设置依赖解析器
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
接口服务层
服务实现层
依赖接口
标签:autofac,Assembly,builder,接口,使用,container,type From: https://www.cnblogs.com/wugh8726254/p/17533413.html