【问题标题】:Unable to schedule tasks by Quartz.NET无法通过 Quartz.NET 安排任务
【发布时间】:2019-05-20 10:36:55
【问题描述】:

这是调度输出任务的主要过程

Public Sub ScheduleOutput()

    Dim sf As ISchedulerFactory = New StdSchedulerFactory()

    Dim scheduler As IScheduler = sf.GetScheduler()
        scheduler.Start()

    Dim job As IJobDetail = JobBuilder.Create(Of OutputJob)().
                WithIdentity("output", "output").Build()

    Dim trigger As ITrigger = TriggerBuilder.Create().
                WithIdentity("trigger", "trigger").ForJob("output").
                WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(setHour.Text, setMinute.Text)).
                Build()
    MsgBox("end")

End Sub

和工作类别

Public Class OutputJob
    Implements IJob

    Public Sub Execute(context As IJobExecutionContext) Implements IJob.Execute

        Output()

    End Sub

    Public Sub Output()

        Dim b = Convert.FromBase64String(HttpContext.Current.Request.Form("encodedhtml"))
        Dim html = System.Text.Encoding.UTF8.GetString(b)
        
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ContentType = "text/html"
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=""Dashboard.html""")
        HttpContext.Current.Response.Write(html)
        HttpContext.Current.Response.End()

    End Sub

End Class

Web.config 文件

<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
        <arg key="configType" value="INLINE"/>
        <arg key="configFile" value="~/log4net.config"/>
        <arg key="level" value="INFO" />
      </factoryAdapter>
    </logging>
  </common>
  <log4net>
    <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %l - %m%n" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="EventLogAppender" />
    </root>
  </log4net>
</configuration>

当我尝试运行代码时,Dim sf As ISchedulerFactory = New StdSchedulerFactory() 出现异常

something.dll 中出现“System.TypeInitializationException”类型的异常,但未在用户代码中处理

附加信息:“Quartz.Impl.StdSchedulerFactory”的类型初始化器引发了异常。

输出中的异常消息(显示在 Visual Studio 底部):

类型的第一次机会异常 Common.Logging.dll 中发生“Common.Logging.ConfigurationException” “System.TypeInitializationException”类型的第一次机会异常 发生在某事.dll中

如何修复异常?

以及代码中可能导致错误/异常的任何其他部分?

我为此苦苦挣扎了很长时间,并搜索了很多解决方案,但它们都不能真正帮助我(或者只是我不知道如何修改以适应我的代码),因为我真的缺乏知识关于任务调度和配置设置。

【问题讨论】:

    标签: asp.net vb.net quartz.net taskscheduler common.logging


    【解决方案1】:

    Quartz 仅依赖于一个名为 Common.Logging 的第三方库(其中包含允许您使用最适合您的日志记录提供程序的日志记录抽象)。您需要在应用二进制文件旁边有 Quartz.dll、Commong.Logging.dll 和 Commong.Logging.Core.dll 才能成功运行 Quartz.NET

    并将 Common.Logging.Log4Net1213.dll 部分替换为:

      <common>
        <logging>
          <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
            <arg key="configType" value="INLINE"/>
            <arg key="level" value="INFO" />
          </factoryAdapter>
        </logging>
      </common>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-3.3.1.0" newVersion="3.3.1.0" />
            </dependentAssembly>
        </assemblyBinding>
      </runtime>
    

    【讨论】:

    • 它在寻找哪个文件?
    • 你应该保留 和 部分并且只替换 一个;这将使用 web.config 文件中的 log4net 配置配置 Common.Logging
    • 我刚刚按照您的建议做了,但随后发生了 System.IO.FileLoadException。 “无法加载文件或程序集 'Common.Logging.Core' 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)”
    • 我从未删除过 和 部分
    • 尝试在 web.config 文件中添加以下内容:
    猜你喜欢
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多