using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Text; using ZY.Mes.Until.ReflexUntil; namespace ZY.Mes.Service { /// /// 接口注入 /// public class InterfaceInject { private IServiceCollection _services; public InterfaceInject(IServiceCollection services) { _services = services; InitDal(); InitService(); } /// /// dal层注入 /// public void InitDal() { var dalDic = AssemblyUntil.GetInterfaceDic("ZY.Mes.DAL", "Dal"); DIInit(dalDic); } /// /// service层注入 /// public void InitService() { var serviceDic= AssemblyUntil.GetInterfaceDic("ZY.Mes.Service", "Service"); DIInit(serviceDic); } /// /// DI注入接口 /// /// public void DIInit(Dictionary assemblyDic) { foreach (var item in assemblyDic) { //transient //每次使用都是新实例 //singleton //每次使用都一样 _services.AddScoped(item.Value, item.Key);//相同请求相同实例 } } } }