public static IServiceCollection AddScopedAssembly(this IServiceCollection service, string interfaceAssemblyName) { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (string.IsNullOrEmpty(interfaceAssemblyName)) { throw new ArgumentNullException(nameof(interfaceAssemblyName)); } var assembly = RuntimeHelper.GetAssembly(interfaceAssemblyName); if (assembly == null) { throw new DllNotFoundException($"the dll \"{interfaceAssemblyName}\" not be found"); } //过滤掉非接口及泛型接口 var types = assembly.GetTypes().Where(t => t.GetTypeInfo().IsInterface&& !t.GetTypeInfo().IsGenericType); foreach (var type in types) { var implementTypeName = type.Name.Substring(1); var implementType = RuntimeHelper.GetImplementType(implementTypeName, type); if (implementType != null) { service.AddScoped(type, implementType); } } return(service); }
public static IServiceCollection RegisterControllers(this IServiceCollection service, string controllerAssemblyName) { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (string.IsNullOrEmpty(controllerAssemblyName)) { throw new ArgumentNullException(nameof(controllerAssemblyName)); } var controllerAssembly = RuntimeHelper.GetAssembly(controllerAssemblyName); if (controllerAssembly == null) { throw new DllNotFoundException($"the dll \"{controllerAssemblyName}\" not be found"); } //过滤掉非接口及泛型接口 var types = controllerAssembly.GetTypes().Where(t => { var typeInfo = t.GetTypeInfo(); return(typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType && t.IsAssignableFrom(typeof(Controller))); }); foreach (var type in types) { service.AddScoped(type); } return(service); }
/// <summary> /// 用DI批量注入接口程序集中对应的实现类。 /// </summary> /// <param name="service"></param> /// <param name="interfaceAssemblyName">接口程序集的名称(不包含文件扩展名)</param> /// <param name="implementAssemblyName">实现程序集的名称(不包含文件扩展名)</param> /// <returns></returns> public static IServiceCollection AddScopedAssembly(this IServiceCollection service, string interfaceAssemblyName, string implementAssemblyName) { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (string.IsNullOrEmpty(interfaceAssemblyName)) { throw new ArgumentNullException(nameof(interfaceAssemblyName)); } if (string.IsNullOrEmpty(implementAssemblyName)) { throw new ArgumentNullException(nameof(implementAssemblyName)); } var interfaceAssembly = RuntimeHelper.GetAssembly(interfaceAssemblyName); if (interfaceAssembly == null) { throw new DllNotFoundException($"the dll \"{interfaceAssemblyName}\" not be found"); } var implementAssembly = RuntimeHelper.GetAssembly(implementAssemblyName); if (implementAssembly == null) { throw new DllNotFoundException($"the dll \"{implementAssemblyName}\" not be found"); } return(service.AddScopeService(interfaceAssembly, implementAssembly)); }
/// <summary> /// Add Transient Assembly. /// </summary> /// <param name="service">服务列表.</param> /// <param name="interfaceAssemblyName">接口程序集的名称(不包含文件扩展名).</param> /// <param name="implementAssemblyName">实现程序集的名称(不包含文件扩展名).</param> /// <returns>IServiceCollection</returns> public static IServiceCollection AddTransientAssembly(this IServiceCollection service, string interfaceAssemblyName, string implementAssemblyName) { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (string.IsNullOrEmpty(interfaceAssemblyName)) { throw new ArgumentNullException(nameof(interfaceAssemblyName)); } if (string.IsNullOrEmpty(implementAssemblyName)) { throw new ArgumentNullException(nameof(implementAssemblyName)); } var interfaceAssembly = RuntimeHelper.GetAssembly(interfaceAssemblyName); if (interfaceAssembly == null) { throw new DllNotFoundException($"the dll \"{interfaceAssemblyName}\" not be found"); } var implementAssembly = RuntimeHelper.GetAssembly(implementAssemblyName); if (implementAssembly == null) { throw new DllNotFoundException($"the dll \"{implementAssemblyName}\" not be found"); } // 过滤掉非接口及泛型接口. var types = interfaceAssembly.GetTypes().Where(t => t.GetTypeInfo().IsInterface&& !t.GetTypeInfo().IsGenericType); foreach (var type in types) { // 过滤掉抽象类、泛型类以及非class. var implementType = implementAssembly.DefinedTypes .FirstOrDefault(t => t.IsClass && !t.IsAbstract && !t.IsGenericType && t.GetInterfaces().Any(b => b.Name == type.Name)); if (implementType != null) { service.AddTransient(type, implementType.AsType()); } } return(service); }
public static IServiceCollection AddScopServiceAssmbly(this IServiceCollection service) { var assemblys = RuntimeHelper.GetServicesAssembly(); foreach (var item in assemblys) { var interfaceAssmbly = RuntimeHelper.GetAssembly(item.GetName().Name + "."); if (interfaceAssmbly == null) { interfaceAssmbly = RuntimeHelper.GetAssembly(item.GetName().Name + "s."); } service = service.AddScopeService(interfaceAssmbly, item); } return(service); }