From 99764c0c5fc83ca068363045b66297f9d4c43c2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Pilgaard=20Gr=C3=B8ndahl?= Date: Sun, 6 Nov 2022 22:07:03 +0100 Subject: [PATCH 1/3] improve result when trimming --- .../Extensions/ServiceCollectionExtensions.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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()) From 617386cf23982d6e59ec940d972a6a5af4f9d369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Pilgaard=20Gr=C3=B8ndahl?= Date: Sun, 6 Nov 2022 22:07:33 +0100 Subject: [PATCH 2/3] nullability issues arose from multi-targeting --- src/Pilgaard.CronJobs/CronBackgroundService.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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); From 04f5483af827925987a5ef9b74437d294a245e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Pilgaard=20Gr=C3=B8ndahl?= Date: Sun, 6 Nov 2022 22:07:51 +0100 Subject: [PATCH 3/3] target both netstandard2.0 and net6.0, change nugets conditionally --- src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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 @@ - + - + + + + + + + + + +