在中等信任主机上使用 quartz.net

本文关键字:quartz net 主机 信任 | 更新日期: 2023-09-27 18:35:37

我需要在我的.NET MVC网站上提供调度功能,我遇到了 Quartz.net 库,它可以完全满足我的需要。

问题是我在主机上运行我的网站(GoDaddy),当我Quartz.net 2.0.1添加到我的项目中时,我遇到了"that assembly does not allow partially trusted callers"例外。经过一些研究,我发现很多人都有同样的问题,有些人通过从 Quartz.net 中删除 Common.Logging 库来解决它。

我遵循了一些建议并删除了对 Common.Logging 的所有引用,但我仍然有问题。看起来这还不够,现在我得到了Inheritance security rules violated while overriding member例外,更多详细信息:

Inheritance security rules violated while overriding member: 
Quartz.Util.DirtyFlagMap`2<TKey,TValue>.GetObjectData
(System.Runtime.Serialization.SerializationInfo, 
System.Runtime.Serialization.StreamingContext)'.
Security accessibility of the overriding method must match the 
security accessibility of the method being overriden.

看起来我真的需要改变一些 Quartz.net 才能让它工作。

有没有人在中等信任度上运行 Quartz.net?如果是这样,需要做什么?也许有人可以建议一些替代方案?

在中等信任主机上使用 quartz.net

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中添加了石英部分,该部分将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中获取的石英部分。

In 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();
     }
  }

我建议自己构建 Common.日志记录,而不是将其从项目中删除。您可以从 http://netcommon.sourceforge.net/downloads.html 获取最新的来源。

我想第二个问题与C5.dll也不可信有关。我也会自己建造。来源可以在这里找到:http://www.itu.dk/research/c5/。

尽管除了构建 dll (http://stackoverflow.com/questions/3072359/unblocking-a-dll-on-a-company-machine-how) 之外还有其他选择,但我个人更喜欢自己构建 dll,除非我绝对信任下载的产品。