Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Would it be possible to run Quartz Scheduler in Console Application #7561


User avatar
0
Jethrob created

I've tried following your sample application to create a seperate Console Application in which to host Quartz Scheduler. AbpHangfireConsoleApp

Would it be possilbe to provide a simple sample application which shows how this is done. The below code throws "Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application."

` public class ExampleTask : JobBase, ITransientDependency { private readonly TenantManager tenantManager;

    public ExampleTask(TenantManager tenantManager)
    {
        this.tenantManager = tenantManager;
    }

    public override Task Execute(IJobExecutionContext context)
    {
        Logger.Debug("Starting ExampleTask.");
       
        foreach (var tenant in tenantManager.Tenants) //Thows Exception
        {
            UnitOfWorkManager.Current.SetTenantId(tenant.Id);

            var value = SettingManager.GetSettingValue<int>("Value");
        }

        return Task.CompletedTask;
    }
}

public sealed class AbpConsoleApplication<TStartupModule> where TStartupModule : AbpModule
{
    /// <summary>
    ///     Gets a reference to the <see cref="AbpBootstrapper" /> instance.
    /// </summary>
    public static AbpBootstrapper AbpBootstrapper { get; } = AbpBootstrapper.Create<TStartupModule>();

    public void Application_Start()
    {
        AbpBootstrapper.Initialize();

        AbpBootstrapper.IocManager.IocContainer.AddFacility<LoggingFacility>(
            f => f.UseAbpLog4Net().WithConfig($"{GetAppPath()}log4net.config")
        );
    }

    private static string GetAppPath()
    {
        string appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
        if (!appPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            appPath += Path.DirectorySeparatorChar;
        return appPath;
    }

    public void Application_End()
    {
        AbpBootstrapper.Dispose();
    }
}
// This runs in Program.cs
private static async Task RunScheduler()
    {
        try
        {
            Console.WriteLine("Scheduler Starting.");

            AbpConsoleApplication<SchedulerModule> app = new AbpConsoleApplication<SchedulerModule>();
            app.Application_Start();

            using (var quartzConfiguration = AbpConsoleApplication<SchedulerModule>.AbpBootstrapper.IocManager.ResolveAsDisposable<IAbpQuartzConfiguration>())
            {
                foreach (var tjob in JobsConfiguration.Jobs())
                {
                    await quartzConfiguration.Object.Scheduler.ScheduleJob(tjob.Job, tjob.Trigger);
                }
            }

            await Task.Delay(-1);
            app.Application_End();
        }
        catch (SchedulerException se)
        {
            Console.WriteLine(se);
        }
    }

`  

No answer yet!