Search code examples
asp.netvb.netscheduled-tasksquartz.netcommon.logging

Unable to schedule tasks by Quartz.NET


Here is the main procedure for scheduling an output task

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

and the job class

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 file

<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>

When I try to run the code, an exception occurred in Dim sf As ISchedulerFactory = New StdSchedulerFactory()

An exception of type 'System.TypeInitializationException' occurred in something.dll but was not handled in user code

Additional information: The type initializer for 'Quartz.Impl.StdSchedulerFactory' threw an exception.

Exception messages in the Output (shown at the bottom of Visual Studio):

A first chance exception of type 'Common.Logging.ConfigurationException' occurred in Common.Logging.dll A first chance exception of type 'System.TypeInitializationException' occurred in something.dll

How can I fix the exception?

And any other parts in the code that can cause errors/exceptions?

I have struggled for long about this and have searched for a lot of solutions, but none of them can actually help me (or just I don't know how to modify in order to fit my code) because I really lack knowledge about task scheduling and configuration settings.


Solution

  • Quartz depends only on a single third-party library called Common.Logging (which contains logging abstractions that allow you to use the logging provider that suites you the best). You need to have Quartz.dll, Commong.Logging.dll and Commong.Logging.Core.dll beside your app binaries to successfully run Quartz.NET

    And replace Common.Logging.Log4Net1213.dll section with:

      <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>