diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs new file mode 100644 index 0000000000..ccb3beac5d --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs @@ -0,0 +1,38 @@ +using System; +using JetBrains.Annotations; +using Microsoft.Extensions.DependencyInjection; + +namespace Volo.Abp.BackgroundWorkers +{ + public static class BackgroundWorkersApplicationInitializationContextExtensions + { + public static ApplicationInitializationContext AddBackgroundWorker([NotNull] this ApplicationInitializationContext context) + where TWorker : IBackgroundWorker + { + Check.NotNull(context, nameof(context)); + + context.AddBackgroundWorker(typeof(TWorker)); + + return context; + } + + public static ApplicationInitializationContext AddBackgroundWorker([NotNull] this ApplicationInitializationContext context, [NotNull] Type workerType) + { + Check.NotNull(context, nameof(context)); + Check.NotNull(workerType, nameof(workerType)); + + if (!workerType.IsAssignableTo()) + { + throw new AbpException($"Given type ({workerType.AssemblyQualifiedName}) must implement the {typeof(IBackgroundWorker).AssemblyQualifiedName} interface, but it doesn't!"); + } + + context.ServiceProvider + .GetRequiredService() + .Add( + (IBackgroundWorker)context.ServiceProvider.GetRequiredService(workerType) + ); + + return context; + } + } +}