Skip to content

Commit

Permalink
Merge pull request #11 from NielsPilgaard/multitarget-net6
Browse files Browse the repository at this point in the history
Multitarget net6.0 / netstandard2.0
  • Loading branch information
NielsPilgaard authored Nov 6, 2022
2 parents 2d6ee55 + 04f5483 commit 4934aff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/Pilgaard.CronJobs/CronBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
22 changes: 19 additions & 3 deletions src/Pilgaard.CronJobs/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,6 +20,9 @@ public static class ServiceCollectionExtensions
/// <param name="types">The types to scan for <see cref="ICronJob"/>s through.</param>
/// <returns>The <see cref="IServiceCollection"/> for further chaining.</returns>
/// <exception cref="ArgumentException">No assemblies found to scan. Supply at least one assembly to scan for Cron Services.</exception>
#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);
Expand All @@ -34,6 +38,9 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, p
/// <param name="assembliesToScan">The assemblies to scan for <see cref="ICronJob"/>s.</param>
/// <returns>The <see cref="IServiceCollection"/> for further chaining.</returns>
/// <exception cref="ArgumentException">No assemblies found to scan. Supply at least one assembly to scan for Cron Services.</exception>
#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);
Expand All @@ -50,6 +57,9 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, p
/// <param name="configuration">The configurator of <see cref="CronJobOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> for further chaining.</returns>
/// <exception cref="ArgumentException">No assemblies found to scan. Supply at least one assembly to scan for Cron Services.</exception>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
#endif
public static IServiceCollection AddCronJobs(this IServiceCollection services, Action<CronJobOptions>? configuration = null, params Type[] types)
{
return services.AddCronJobs(types.Select(type => type.GetTypeInfo().Assembly), configuration);
Expand All @@ -66,6 +76,9 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, A
/// <param name="configuration">The configurator of <see cref="CronJobOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> for further chaining.</returns>
/// <exception cref="ArgumentException">No assemblies found to scan. Supply at least one assembly to scan for Cron Services.</exception>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
#endif
public static IServiceCollection AddCronJobs(this IServiceCollection services, Action<CronJobOptions>? configuration = null, params Assembly[] assembliesToScan)
{
return services.AddCronJobs(assembliesToScan, configuration);
Expand All @@ -82,8 +95,11 @@ public static IServiceCollection AddCronJobs(this IServiceCollection services, A
/// <param name="configurationAction">The configurator of <see cref="CronJobOptions"/>.</param>
/// <returns>The <see cref="IServiceCollection"/> for further chaining.</returns>
/// <exception cref="ArgumentException">No assemblies found to scan. Supply at least one assembly to scan for Cron Services.</exception>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Calls System.Reflection.Assembly.ExportedTypes")]
#endif
private static IServiceCollection AddCronJobs(this IServiceCollection services,
IEnumerable<Assembly> assembliesToScan,
IEnumerable<Assembly> assembliesToScan,
Action<CronJobOptions>? configurationAction)
{
if (!assembliesToScan.Any())
Expand Down
15 changes: 12 additions & 3 deletions src/Pilgaard.CronJobs/Pilgaard.CronJobs.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Authors>Niels Pilgaard</Authors>
<Description>Easily create jobs that execute based on Cron expressions.</Description>
<Copyright>Copyright Niels Pilgaard</Copyright>
Expand All @@ -23,11 +23,20 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath=""/>
<None Include="..\..\README.md" Pack="true" PackagePath="" />
<None Include="..\..\assets\logo\logo_128x128.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="2.5.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.18" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.18" />
Expand Down

0 comments on commit 4934aff

Please sign in to comment.