Files
InServiceQue/InServiceQue.Sample/HostedServiceRegistrator.cs
2024-03-10 00:43:57 +07:00

19 lines
785 B
C#

using System.Reflection;
namespace InServiceQue.Sample;
public class HostedServiceRegistrator
{
public void RegisterHostedService(IServiceCollection services, Type hostedServiceType)
{
Type servicesType = typeof(HostedServiceRegistrator);
MethodInfo methodInfo = servicesType.GetMethod("AddHostedService");
MethodInfo genericMethod = methodInfo.MakeGenericMethod(hostedServiceType);
genericMethod.Invoke(this, new object[] { services });
}
// Needed as a work-arround because we can't call the extension method with reflection.
public IServiceCollection AddHostedService<THostedService>(IServiceCollection services)
where THostedService : class, IHostedService =>
services.AddHostedService<THostedService>();
}