ScheduleJob方法的FileLoadException

时间:2014-10-21 09:55:19

标签: c# windows-services quartz.net

我已成功加载Quartz.NET Windows服务的调度程序,我可以看到它当前正在运行一个示例作业。 但是当我尝试将作业添加到调度程序时(在下面的sched.ScheduleJob(job, trigger)行上),我得到一个FileLoadException异常,其中包含:

  

无法加载文件或程序集' Quartz,Version = 2.2.4.400,   Culture = neutral,PublicKeyToken = f6b8c98a402cc8a4'或其中一个   依赖。定位程序集的清单定义没有   匹配程序集引用。 (HRESULT异常:0x80131040)

这是我用来获取我的调度程序并添加作业和触发器的代码:

NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteClient";

// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "10";
properties["quartz.threadPool.threadPriority"] = "Normal";

// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:555/QuartzScheduler";

ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();
Console.WriteLine("name: " + sched.SchedulerName);
Console.WriteLine("instance id: " + sched.SchedulerInstanceId);
Console.WriteLine("is started: " + sched.IsStarted);
Console.WriteLine("context : " + sched.Context);
var job = JobBuilder.Create()
    .WithIdentity("MyEmailJob", "JobCategory1")
    .RequestRecovery().Build();
var trigger = (ICronTrigger)TriggerBuilder.Create() 
        .WithIdentity("MyEmailJob", "JobCategory1") 
        .WithCronSchedule("0 0/5 * 1/1 * ? *") 
        .StartAt(DateTime.Now) .WithPriority(1).Build();
var schedule = sched.ScheduleJob(job, trigger);

App.config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>

  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>

  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %l - %m%n" />
      </layout>
    </appender>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="service-log.txt" />
      <appendToFile value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="FileAppender" />
      <!-- uncomment to enable event log appending -->
      <!-- <appender-ref ref="EventLogAppender" /> -->
    </root>
  </log4net>

  <!-- 
    We use quartz.config for this server, you can always use configuration section if you want to.
    Configuration section has precedence here.  
  -->
  <!--
  <quartz >
  </quartz>
  -->
</configuration>

1 个答案:

答案 0 :(得分:1)

这是一个程序集不匹配问题,而不是与quartz.net有关。这意味着您的应用程序无法在运行时找到Quartz.dll的确切版本。 bin文件夹中的版本与项目引用的版本不同。

尝试从项目中删除引用并重新添加它。选择您的Windows服务使用的那个。清洁解决方案,重建。

如果你使用nuget安装了Quartz,你需要先通过nuget卸载它。