79261800

Date: 2024-12-08 03:29:56
Score: 0.5
Natty:
Report link

The answer from @maxim helped me. I ended up creating this extension class to load all services derived from the base class ProcessorBase automatically. Sharing in case it helps

public static class ServiceCollectionExtensions
{        
    public static IServiceCollection AddJobProcessors(this IServiceCollection services, Assembly assembly = null)
    {
        assembly ??= Assembly.GetCallingAssembly();

        var processorTypes = assembly.GetTypes()
            .Where(t => t.IsClass
                && !t.IsAbstract
                && t.BaseType != null
                && t.BaseType.IsGenericType
                && t.BaseType.GetGenericTypeDefinition() == typeof(ProcessorBase<>));

        foreach (var processor in processorTypes)            
            services.AddTransient(typeof(IHostedService), processor);            

        return services;
    }
}    

Usage:

services.AddJobProcessors();
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @maxim
  • Low reputation (0.5):
Posted by: rbohac