减少 startup 内的 Add和 Use

This commit is contained in:
MysticBoy
2020-06-06 09:48:24 +08:00
parent 940d118439
commit c6f44fa1ef
8 changed files with 73 additions and 49 deletions

View File

@@ -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"
}
}
}
}

View File

@@ -32,8 +32,7 @@ namespace AspNetCore31
services.AddOptions();
services.Configure<AppSettings>(Configuration);
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
services.AddQuartzHostedService()
.AddQuartzJob<HelloJob>()
services.AddQuartzJob<HelloJob>()
.AddQuartzJob<InjectSampleJob>()
.AddQuartzJob<HelloJobSingle>()
.AddQuartzJob<InjectSampleJobSingle>();

11
sample/app.config Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueFileSectionHandler" />
</configSections>
<quartz>
<add key="quartz.plugin.recentHistory.type" value="Quartz.Plugins.RecentHistory.ExecutionHistoryPlugin, Quartz.Plugins.RecentHistory" />
<add key="quartz.plugin.recentHistory.storeType" value="Quartz.Plugins.RecentHistory.Impl.InProcExecutionHistoryStore, Quartz.Plugins.RecentHistory" />
</quartz>
</configuration>

View File

@@ -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<Services> configure = null )
public static void UseSilkierQuartz(this IApplicationBuilder app, SilkierQuartzOptions options, Action<Services> 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 ) =>
app.Use(async (context, next) =>
{
context.Items[typeof( Services )] = services;
context.Items[typeof(Services)] = services;
await next.Invoke();
} );
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(nameof(SilkierQuartz), $"{options.VirtualPathRoot}/{{controller=Scheduler}}/{{action=Index}}");
});
#if NETCOREAPP
app.UseEndpoints( endpoints =>
{
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
}
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<NameValueCollection> 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

View File

@@ -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
{

View File

@@ -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
{

View File

@@ -77,7 +77,7 @@
{{#footer}}
<footer style="text-align:center; margin-top: 3em; padding: .8em 0; background-color: #f8f8f8; font-size: 90%; border-top: 1px solid rgba(34,36,38,.1);">
<a href="https://github.com/jlucansky/SilkierQuartz" target="_blank" style="color: #909090"><i class="github icon"></i> SilkierQuartz&nbsp;&nbsp;v{{SilkierQuartzVersion}}</a>
<a href="https://github.com/maikebing/SilkierQuartz" target="_blank" style="color: #909090"><i class="github icon"></i> SilkierQuartz&nbsp;&nbsp;v{{SilkierQuartzVersion}}</a>
</footer>
{{/footer}}
</body>