forked from iamoldli/NetModular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInitializer.cs
More file actions
97 lines (87 loc) · 3.36 KB
/
ModuleInitializer.cs
File metadata and controls
97 lines (87 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Collections.Specialized;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Nm.Lib.Data.Abstractions.Enums;
using Nm.Lib.Data.Abstractions.Options;
using Nm.Lib.Module.AspNetCore;
using Nm.Lib.Quartz.Abstractions;
using Nm.Lib.Quartz.Web;
using Nm.Lib.Utils.Core.Extensions;
using Nm.Module.Quartz.Infrastructure.Options;
using Nm.Module.Quartz.Web.Core;
using Quartz;
namespace Nm.Module.Quartz.Web
{
public class ModuleInitializer : IModuleInitializer
{
/// <summary>
/// 注入服务
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IJobLogger, JobLogger>();
services.AddSingleton<ISchedulerListener, SchedulerListener>();
var quartzProps = GetQuartzProps(services);
services.AddQuartz(quartzProps);
}
/// <summary>
/// 配置中间件
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
}
/// <summary>
/// 配置MVC功能
/// </summary>
/// <param name="mvcOptions"></param>
public void ConfigureMvc(MvcOptions mvcOptions)
{
}
/// <summary>
/// 获取Quartz属性
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
private NameValueCollection GetQuartzProps(IServiceCollection services)
{
var sp = services.BuildServiceProvider();
var options = sp.GetService<IOptionsMonitor<QuartzOptions>>().CurrentValue;
var quartzProps = new NameValueCollection();
var quartzDbOptions = sp.GetService<DbOptions>().Connections.FirstOrDefault(m => m.Name.EqualsIgnoreCase("quartz"));
if (quartzDbOptions != null)
{
quartzProps["quartz.scheduler.instanceName"] = options.InstanceName;
quartzProps["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX,Quartz";
quartzProps["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate,Quartz";
quartzProps["quartz.jobStore.tablePrefix"] = options.TablePrefix;
quartzProps["quartz.jobStore.dataSource"] = "default";
quartzProps["quartz.dataSource.default.connectionString"] = quartzDbOptions.ConnString;
quartzProps["quartz.dataSource.default.provider"] = GetProvider(quartzDbOptions.Dialect);
quartzProps["quartz.serializer.type"] = options.SerializerType;
}
return quartzProps;
}
private string GetProvider(SqlDialect dialect)
{
switch (dialect)
{
case SqlDialect.SqlServer:
return "SqlServer";
case SqlDialect.MySql:
return "MySql";
case SqlDialect.SQLite:
return "SQLite-10";
case SqlDialect.Oracle:
return "OracleODP";
}
return "";
}
}
}