Hello you can try this way
Add Extension Method
public static class SlightDIModuleConfigExtension
{
public static IWebHostBuilder UseSlightDIModuleconfig(this IWebHostBuilder webHostBuilder)
{
return webHostBuilder.ConfigureServices(services =>
{
foreach (Type type in Assembly.GetEntryAssembly()
.GetTypes()
.Where(myType => myType.IsSubclassOf(typeof(SlightModuleConfigure))))
{
var instance = Activator.CreateInstance(type);
MethodInfo mi = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault();
mi.Invoke(instance, new object[] { services });
}
});
}
}
Add abstract class
public abstract class SlightModuleConfigure
{
protected virtual void Load(IServiceCollection services) { }
}
How to Use
in Program.cs add UseSlightDIModuleconfig()
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSlightDIModuleconfig(); //add this .
Add class inherit SlightModuleConfigure like this
public class PayModuleConfig : SlightModuleConfigure
{
protected override void Load(IServiceCollection services)
{
services.AddTransient<Ipay, PayImp>();
}
}
you can refer to SlightDIConfigure
Startup
class. – Moppet