Steinar 的回答让我朝着正确的方向前进。在此处分享使 QuartZNet 在中等信任托管环境中工作的步骤。
QuartzNet 最初遇到了中等信任的权限问题,我们需要执行以下操作来解决问题
(1) 从 github 下载 QuartzNet 代码 ( 2.1.0.400 ) 并在对 AssemblyInfo.cs 进行以下更改后构建它
更换
#if !NET_40
[assembly: System.Security.AllowPartiallyTrustedCallers]
#endif
和
[assembly: AllowPartiallyTrustedCallers]
#if NET_40
[assembly: SecurityRules(SecurityRuleSet.Level1)]
#endif
(2) 下载 C5 代码 (v 2.1) 并使用
[assembly: AllowPartiallyTrustedCallersAttribute()
确保 C5 在与 Qartznet 相同的 .NET 版本中编译。
(3) 在 TGH 中的 web.config 中添加了 quartz 部分,该部分的 requirepermission 设置为 false。通用日志记录部分也将 requirepermission 设置为 false,还将其配置为使用 Common.Logging.Simple.NoOpLoggerFactoryAdapter。
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" requirePermission="false" />
</sectionGroup>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.NoOpLoggerFactoryAdapter, Common.Logging">
<arg key="showLogName" value="true" />
<arg key="showDataTime" value="true" />
<arg key="level" value="OFF" />
<arg key="dateTimeFormat" value="HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
<quartz>
<add key="quartz.scheduler.instanceName" value="QuartzScheduler" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPriority" value="2" />
<add key="quartz.jobStore.misfireThreshold" value="60000" />
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
</quartz>
(4) 使用以namecollection为参数的构造函数初始化调度器,namecollection是从web.config中提取的quartz部分。
在 global.asax
QuartzScheduler.Start();
班上
public class QuartzScheduler
{
public static void Start()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory((NameValueCollection)ConfigurationManager.GetSection("quartz"));
IScheduler scheduler = schedulerFactory.GetScheduler();
scheduler.Start();
IJobDetail inviteRequestProcessor = new JobDetailImpl("ProcessInviteRequest", null, typeof(InviteRequestJob));
IDailyTimeIntervalTrigger trigger = new DailyTimeIntervalTriggerImpl("Invite Request Trigger", Quartz.TimeOfDay.HourMinuteAndSecondOfDay(0, 0, 0), Quartz.TimeOfDay.HourMinuteAndSecondOfDay(23, 23, 59), Quartz.IntervalUnit.Second, 1);
scheduler.ScheduleJob(inviteRequestProcessor, trigger);
}
}
public class InviteRequestJob : IJob
{
public void Execute(IJobExecutionContext context)
{
RequestInvite.ProcessInviteRequests();
}
}