在中等信任托管上使用quartz.net

时间:2012-05-28 20:02:02

标签: c# .net scheduling quartz.net quartz.net-2.0

我需要在我的.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?如果是这样,需要做什么?可能有人可以提出一些替代方案吗?

2 个答案:

答案 0 :(得分:4)

斯坦纳的回答让我朝着正确的方向前进。分享这里让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中获取的石英部分。

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

答案 1 :(得分:1)

我建议您自己构建Common.Logging,而不是从项目中删除它。您可以从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,除非我完全相信下载的产品。

相关问题