一、安装NuGet包
二、创建项目名称+Module的类
三、继承AutoFac.Modeule
四、重写Load方法,完成自动注入
protected override void Load(ContainerBuilder builder) { //1、动态加载程序集 var assembly = Assembly.Load("NewRBAC.Repository"); //2.注册 builder.RegisterAssemblyTypes(assembly) //查找以Repository结尾的文件 .Where(x=>x.Name.EndsWith("Repository")) //自动加载接口 .AsImplementedInterfaces(); //1、动态加载程序集 var assemblyService = Assembly.Load("NewRBAC.Service"); //2.注册 builder.RegisterAssemblyTypes(assemblyService) //查找以Repository结尾的文件 .Where(x => x.Name.EndsWith("Service")) //自动加载接口 .AsImplementedInterfaces(); builder.RegisterType<TokenHelper>().SingleInstance(); builder.RegisterType<JwtSecurityTokenHandler>().SingleInstance(); base.Load(builder); }
五、Program中注册
标签:Load,Core,AutoFac,Service,Repository,builder,net,加载 From: https://www.cnblogs.com/xu12150501/p/17656851.html