diff --git a/src/Pilgaard.CronJobs/CronBackgroundService.cs b/src/Pilgaard.CronJobs/CronBackgroundService.cs
index a187b7d..f686e99 100644
--- a/src/Pilgaard.CronJobs/CronBackgroundService.cs
+++ b/src/Pilgaard.CronJobs/CronBackgroundService.cs
@@ -46,7 +46,12 @@ public CronBackgroundService(
// Lookup CronJob to get it's schedule without compromising its lifecycle
// If lifetime is set to Singleton, the CronJob remains un-disposed.
using var scope = _serviceScopeFactory.CreateScope();
- _cronJob = (ICronJob)scope.ServiceProvider.GetService(cronJob.GetType());
+ var scopedCronJob = scope.ServiceProvider.GetService(cronJob.GetType());
+
+ _cronJob = (ICronJob?)scopedCronJob ?? throw new ArgumentNullException(
+ nameof(cronJob),
+ $"Failed to GetService of type {cronJob.GetType().FullName} from ServiceProvider. " +
+ "Remember to register it in the ServiceCollection.");
_cronJobName = _cronJob.GetType().Name;
_cronSchedule = _cronJob.CronSchedule;
@@ -122,7 +127,7 @@ private async Task GetScopedJobAndExecute(CancellationToken stoppingToken)
using var scope = _serviceScopeFactory.CreateScope();
- var cronJob = (ICronJob)scope.ServiceProvider.GetService(_cronJob.GetType());
+ var cronJob = (ICronJob)scope.ServiceProvider.GetService(_cronJob.GetType())!;
await cronJob.ExecuteAsync(stoppingToken);
diff --git a/src/Pilgaard.CronJobs/Extensions/ServiceCollectionExtensions.cs b/src/Pilgaard.CronJobs/Extensions/ServiceCollectionExtensions.cs
index 2cfa821..929bd16 100644
--- a/src/Pilgaard.CronJobs/Extensions/ServiceCollectionExtensions.cs
+++ b/src/Pilgaard.CronJobs/Extensions/ServiceCollectionExtensions.cs
@@ -1,9 +1,10 @@
-using System.Reflection;
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Pilgaard.CronJobs.Configuration;
+using System.Diagnostics.CodeAnalysis;
+using System.Reflection;
namespace Pilgaard.CronJobs.Extensions;
@@ -19,6 +20,9 @@ public static class ServiceCollectionExtensions
/// The types to scan for s through.
/// The for further chaining.
/// No assemblies found to scan. Supply at least one assembly to scan for Cron Services.
+#if NET6_0_OR_GREATER
+ [RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
+#endif
public static IServiceCollection AddCronJobs(this IServiceCollection services, params Type[] types)
{
return services.AddCronJobs(types.Select(type => type.GetTypeInfo().Assembly), null);
@@ -34,6 +38,9 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, p
/// The assemblies to scan for s.
/// The for further chaining.
/// No assemblies found to scan. Supply at least one assembly to scan for Cron Services.
+#if NET6_0_OR_GREATER
+ [RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
+#endif
public static IServiceCollection AddCronJobs(this IServiceCollection services, params Assembly[] assembliesToScan)
{
return services.AddCronJobs(assembliesToScan, null);
@@ -50,6 +57,9 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, p
/// The configurator of .
/// The for further chaining.
/// No assemblies found to scan. Supply at least one assembly to scan for Cron Services.
+#if NET6_0_OR_GREATER
+ [RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
+#endif
public static IServiceCollection AddCronJobs(this IServiceCollection services, Action? configuration = null, params Type[] types)
{
return services.AddCronJobs(types.Select(type => type.GetTypeInfo().Assembly), configuration);
@@ -66,6 +76,9 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, A
/// The configurator of .
/// The for further chaining.
/// No assemblies found to scan. Supply at least one assembly to scan for Cron Services.
+#if NET6_0_OR_GREATER
+ [RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
+#endif
public static IServiceCollection AddCronJobs(this IServiceCollection services, Action? configuration = null, params Assembly[] assembliesToScan)
{
return services.AddCronJobs(assembliesToScan, configuration);
@@ -82,8 +95,11 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, A
/// The configurator of .
/// The for further chaining.
/// No assemblies found to scan. Supply at least one assembly to scan for Cron Services.
+#if NET6_0_OR_GREATER
+ [RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
+#endif
private static IServiceCollection AddCronJobs(this IServiceCollection services,
- IEnumerable assembliesToScan,
+ IEnumerable assembliesToScan,
Action? configurationAction)
{
if (!assembliesToScan.Any())
diff --git a/src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj b/src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj
index ec69233..cebf464 100644
--- a/src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj
+++ b/src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj
@@ -1,7 +1,7 @@
- netstandard2.0
+ netstandard2.0;net6.0
Niels Pilgaard
Easily create jobs that execute based on Cron expressions.
Copyright Niels Pilgaard
@@ -23,11 +23,20 @@
-
+
-
+
+
+
+
+
+
+
+
+
+