From c6f44fa1ef350069970fe63c876a4f763f2164d5 Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Sat, 6 Jun 2020 09:48:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91=20startup=20=E5=86=85?= =?UTF-8?q?=E7=9A=84=20Add=E5=92=8C=20Use?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/Properties/launchSettings.json | 27 +++++++ sample/Startup.cs | 3 +- sample/app.config | 11 +++ .../ApplicationBuilderExtensions.cs | 73 ++++++++----------- .../IJobRegistratorExtensions.cs | 3 +- .../IServiceCollectionExtensions.cs | 3 +- ...zminOptions.cs => SilkierQuartzOptions.cs} | 0 src/SilkierQuartz/Views/Layout.hbs | 2 +- 8 files changed, 73 insertions(+), 49 deletions(-) create mode 100644 sample/Properties/launchSettings.json create mode 100644 sample/app.config rename src/SilkierQuartz/{QuartzminOptions.cs => SilkierQuartzOptions.cs} (100%) diff --git a/sample/Properties/launchSettings.json b/sample/Properties/launchSettings.json new file mode 100644 index 0000000..f69258d --- /dev/null +++ b/sample/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:64450", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "SilkierQuartz.Example": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/sample/Startup.cs b/sample/Startup.cs index 42d6cdf..ca751de 100644 --- a/sample/Startup.cs +++ b/sample/Startup.cs @@ -32,8 +32,7 @@ namespace AspNetCore31 services.AddOptions(); services.Configure(Configuration); services.Configure(options => { options.WriteText = "This is inject string"; }); - services.AddQuartzHostedService() - .AddQuartzJob() + services.AddQuartzJob() .AddQuartzJob() .AddQuartzJob() .AddQuartzJob(); diff --git a/sample/app.config b/sample/app.config new file mode 100644 index 0000000..31e49cd --- /dev/null +++ b/sample/app.config @@ -0,0 +1,11 @@ + + + +
+ + + + + + + diff --git a/src/SilkierQuartz/ApplicationBuilderExtensions.cs b/src/SilkierQuartz/ApplicationBuilderExtensions.cs index b9df508..2249e24 100644 --- a/src/SilkierQuartz/ApplicationBuilderExtensions.cs +++ b/src/SilkierQuartz/ApplicationBuilderExtensions.cs @@ -1,85 +1,70 @@ -#if ( NETSTANDARD || NETCOREAPP ) - + using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; +using SilkierQuartz; using System; +using System.Collections.Specialized; using System.Reflection; namespace SilkierQuartz { public static class ApplicationBuilderExtensions { - public static void UseSilkierQuartz( this IApplicationBuilder app, SilkierQuartzOptions options, Action configure = null ) + public static void UseSilkierQuartz(this IApplicationBuilder app, SilkierQuartzOptions options, Action configure = null) { - options = options ?? throw new ArgumentNullException( nameof( options ) ); + options = options ?? throw new ArgumentNullException(nameof(options)); - app.UseFileServer( options ); + app.UseFileServer(options); - var services = Services.Create( options ); - configure?.Invoke( services ); + var services = Services.Create(options); + configure?.Invoke(services); - app.Use( async ( context, next ) => - { - context.Items[typeof( Services )] = services; - await next.Invoke(); - } ); - - -#if NETCOREAPP - app.UseEndpoints( endpoints => + app.Use(async (context, next) => { - endpoints.MapControllerRoute( nameof( SilkierQuartz ), $"{options.VirtualPathRoot}/{{controller=Scheduler}}/{{action=Index}}" ); - } ); -#else - app.UseMvc( routes => - { - routes.MapRoute( - name: nameof( SilkierQuartz ), - template: "{controller=Scheduler}/{action=Index}" ); - } ); -#endif + context.Items[typeof(Services)] = services; + await next.Invoke(); + }); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute(nameof(SilkierQuartz), $"{options.VirtualPathRoot}/{{controller=Scheduler}}/{{action=Index}}"); + }); + } - private static void UseFileServer( this IApplicationBuilder app, SilkierQuartzOptions options ) + private static void UseFileServer(this IApplicationBuilder app, SilkierQuartzOptions options) { IFileProvider fs; - if ( string.IsNullOrEmpty( options.ContentRootDirectory ) ) - fs = new ManifestEmbeddedFileProvider( Assembly.GetExecutingAssembly(), "Content" ); + if (string.IsNullOrEmpty(options.ContentRootDirectory)) + fs = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly(), "Content"); else - fs = new PhysicalFileProvider( options.ContentRootDirectory ); + fs = new PhysicalFileProvider(options.ContentRootDirectory); var fsOptions = new FileServerOptions() { - RequestPath = new PathString( $"{options.VirtualPathRoot}/Content" ), + RequestPath = new PathString($"{options.VirtualPathRoot}/Content"), EnableDefaultFiles = false, EnableDirectoryBrowsing = false, FileProvider = fs }; - app.UseFileServer( fsOptions ); + app.UseFileServer(fsOptions); } -#if NETCOREAPP - public static void AddSilkierQuartz( this IServiceCollection services ) + public static void AddSilkierQuartz(this IServiceCollection services, Action stdSchedulerFactoryOptions = null) { services.AddControllers() - .AddApplicationPart( Assembly.GetExecutingAssembly() ) + .AddApplicationPart(Assembly.GetExecutingAssembly()) .AddNewtonsoftJson(); + services.UseQuartzHostedService(stdSchedulerFactoryOptions); + } -#else - public static void AddSilkierQuartz( this IServiceCollection services ) - { - services.AddMvcCore() - .AddApplicationPart( Assembly.GetExecutingAssembly() ) - .AddJsonFormatters(); - } -#endif + } } -#endif diff --git a/src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs b/src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs index a2b0d78..06b23a8 100644 --- a/src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs +++ b/src/SilkierQuartz/HostedService/IJobRegistratorExtensions.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using Quartz; using Quartz.Spi; +using SilkierQuartz.HostedService; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +10,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -namespace SilkierQuartz.HostedService +namespace SilkierQuartz { public static class IJobRegistratorExtensions { diff --git a/src/SilkierQuartz/HostedService/IServiceCollectionExtensions.cs b/src/SilkierQuartz/HostedService/IServiceCollectionExtensions.cs index 09246e5..b462eb6 100644 --- a/src/SilkierQuartz/HostedService/IServiceCollectionExtensions.cs +++ b/src/SilkierQuartz/HostedService/IServiceCollectionExtensions.cs @@ -4,12 +4,13 @@ using Microsoft.Extensions.Hosting; using Quartz; using Quartz.Impl; using Quartz.Spi; +using SilkierQuartz.HostedService; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; -namespace SilkierQuartz.HostedService +namespace SilkierQuartz { public static class IServiceCollectionExtensions { diff --git a/src/SilkierQuartz/QuartzminOptions.cs b/src/SilkierQuartz/SilkierQuartzOptions.cs similarity index 100% rename from src/SilkierQuartz/QuartzminOptions.cs rename to src/SilkierQuartz/SilkierQuartzOptions.cs diff --git a/src/SilkierQuartz/Views/Layout.hbs b/src/SilkierQuartz/Views/Layout.hbs index 5d6283a..d7fcbe4 100644 --- a/src/SilkierQuartz/Views/Layout.hbs +++ b/src/SilkierQuartz/Views/Layout.hbs @@ -77,7 +77,7 @@ {{#footer}} {{/footer}}