Files
InServiceQue/InServiceQue.Sample/HostedServiceRegistrator.cs
2024-03-10 18:20:41 +07:00

19 lines
693 B
C#

using System.Reflection;
namespace InServiceQue.Sample;
public class HostedServiceRegistrator
{
public void RegisterHostedService(IServiceCollection services, Type hostedServiceType)
{
var servicesType = typeof(HostedServiceRegistrator);
var methodInfo = servicesType.GetMethod(nameof(AddHostedService));
var genericMethod = methodInfo.MakeGenericMethod(hostedServiceType);
genericMethod.Invoke(this, new object[] { services });
}
public IServiceCollection AddHostedService<THostedService>(IServiceCollection services)
where THostedService : class, IHostedService =>
services.AddHostedService<THostedService>();
}