Skip to content

Commit 693c073

Browse files
maikebingmukmyash
authored andcommitted
1 parent f86f3b5 commit 693c073

File tree

6 files changed

+47
-12
lines changed

6 files changed

+47
-12
lines changed

sample/AspNetCoreSampleQuartzHostedService/AspNetCoreSampleQuartzHostedService.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="CrystalQuartz.AspNetCore" Version="6.8.1" />
89
</ItemGroup>
910

1011
<ItemGroup>

sample/AspNetCoreSampleQuartzHostedService/Program.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@
66
using Microsoft.AspNetCore;
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Hosting;
911
using Microsoft.Extensions.Logging;
12+
using QuartzHostedService;
1013

1114
namespace AspNetCoreSampleQuartzHostedService
1215
{
1316
public class Program
1417
{
1518
public static void Main(string[] args)
1619
{
17-
CreateWebHostBuilder(args).Build().Run();
20+
CreateHostBuilder(args).Build().Run();
1821
}
1922

20-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21-
WebHost.CreateDefaultBuilder(args)
22-
.UseStartup<Startup>();
23+
public static IHostBuilder CreateHostBuilder(string[] args) =>
24+
Host.CreateDefaultBuilder(args)
25+
.ConfigureWebHostDefaults(webBuilder =>
26+
{
27+
webBuilder.UseStartup<Startup>();
28+
webBuilder.ConfigureKestrel(serverOptions =>
29+
{
30+
serverOptions.AllowSynchronousIO = true;
31+
});
32+
})
33+
.ConfigureQuartzHost();
2334
}
2435
}
36+

sample/AspNetCoreSampleQuartzHostedService/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using AspNetCoreSampleQuartzHostedService.Jobs;
6+
using CrystalQuartz.AspNetCore;
67
using Microsoft.AspNetCore.Builder;
78
using Microsoft.AspNetCore.Hosting;
89
using Microsoft.AspNetCore.Mvc;
@@ -40,7 +41,7 @@ public void ConfigureServices(IServiceCollection services)
4041
}
4142

4243
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
43-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<AppSettings> settings)
44+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<AppSettings> settings, ISchedulerFactory factory)
4445
{
4546
if (env.IsDevelopment())
4647
{
@@ -51,6 +52,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions
5152
{
5253
endpoint.MapControllers();
5354
});
55+
app.UseCrystalQuartz(() => factory.GetScheduler().Result);
5456
if (settings.Value.EnableHelloSingleJob)
5557
{
5658
app.UseQuartzJob<HelloJobSingle>(TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()))
@@ -77,6 +79,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions
7779
.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()));
7880
return result;
7981
});
82+
8083
}
8184
}
8285

sample/SampleQuartzHostedService/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static async Task Main(string[] args)
1818
{
1919
services.AddOptions();
2020
services.Configure<InjectProperty>(options=> { options.WriteText = "This is inject string"; });
21-
2221
services.UseQuartzHostedService()
2322
.RegiserJob<HelloJob>(() =>
2423
{
@@ -36,7 +35,7 @@ public static async Task Main(string[] args)
3635
.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()));
3736
return result;
3837
});
39-
});
38+
}).ConfigureQuartzHost();
4039

4140
await hostBuilder.RunConsoleAsync();
4241
}

src/QuartzHostedService/IServiceCollectionExtensions.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Hosting;
24
using Quartz;
35
using Quartz.Impl;
46
using Quartz.Spi;
@@ -26,11 +28,29 @@ public static IServiceCollection AddQuartzHostedService(this IServiceCollection
2628
var re = services.UseQuartzHostedService(stdSchedulerFactoryOptions);
2729
return re.Services;
2830
}
31+
32+
private static bool _quartzHostedServiceIsAdded = false;
33+
/// <summary>
34+
/// Must be call after Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults()
35+
/// </summary>
36+
/// <param name="builder"></param>
37+
/// <seealso cref="https://github.com/aspnet/AspNetCore.Docs/issues/14381"/>
38+
/// <seealso cref="https://github.com/aspnet/AspNetCore/pull/11575"/>
39+
/// <returns></returns>
40+
public static IHostBuilder ConfigureQuartzHost(this IHostBuilder builder)
41+
{
42+
_quartzHostedServiceIsAdded = true;
43+
return builder.ConfigureServices(services => services.AddHostedService<QuartzHostedService>());
44+
}
45+
46+
2947
public static IJobRegistrator UseQuartzHostedService(this IServiceCollection services,
3048
Action<NameValueCollection> stdSchedulerFactoryOptions)
3149
{
32-
services.AddHostedService<QuartzHostedService>();
33-
50+
if (!_quartzHostedServiceIsAdded)
51+
{
52+
services.AddHostedService<QuartzHostedService>();
53+
}
3454
services.AddTransient<ISchedulerFactory>(provider =>
3555
{
3656
var options = new NameValueCollection();
@@ -41,7 +61,6 @@ public static IJobRegistrator UseQuartzHostedService(this IServiceCollection ser
4161
return result;
4262
});
4363
services.AddSingleton<IJobFactory, ServiceCollectionJobFactory>();
44-
4564
return new JobRegistrator(services);
4665
}
4766
}

src/QuartzHostedService/QuartzHostedService.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<Authors>mukmyash</Authors>
66
<Company />
7-
<Version>0.0.5</Version>
7+
<Version>0.0.6</Version>
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<RepositoryUrl>https://github.com/mukmyash/QuartzHostedService</RepositoryUrl>
1010
<Description>Wrapper above [Quartz.NET] (https://github.com/quartznet/quartznet) for .NET Core.</Description>
1111
<PackageTags>scheduling tasks jobs triggers scheduler threading quartz</PackageTags>
12+
<PackageId>QuartzHostedServiceEx</PackageId>
1213
</PropertyGroup>
1314

1415
<ItemGroup>

0 commit comments

Comments
 (0)