From acc2450b09d25ae9898e4b9701b137a073b44cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 17 Mar 2020 20:25:10 +0300 Subject: [PATCH] Create BackgroundWorkersApplicationInitializationContextExtensions.cs --- ...licationInitializationContextExtensions.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs 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; + } + } +}