Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to achieve one thing: run .NET Core web application (with kestrel) and run Quartz.Net process that would execute some methods. But all my tries have been unsuccesfull. I have them both working separately, but I can't run them at the same time.

My process is running all the time on linux server where it has to check for database changes. I needed to add web page in order to see print out results. So I want to be able to view these results on web (localhost:5000) but also for the process to keep running.

Is it possible to do what I am trying to achieve? (start web and console process AND have them both working as long as the program is running)?

What I have tried:

this is my `Startup.cs ` class:
public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton();
        services.AddLogging();
        var serviceProvider = services.BuildServiceProvider();
        services.AddMvc();
        services.AddSingleton(Configuration);
        return WindsorRegistrationHelper.CreateServiceProvider(ContainerManager.Container, services);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

    }


and this is my Program.cs class. Web work now, however the process is not started. The commented code is one of many ways I've tried to run both, but failed.

class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();   
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        return host = new WebHostBuilder()
            .UseKestrel()
            .UseConfiguration(config)
            .UseStartup()
           .Build();
    }

    //static void Main(string[] args)
    //{
    //    var ConsOut = Console.Out; 
    //    Console.SetOut(new StreamWriter(Stream.Null));
    //    BuildWebHost(args).Start();                    
    //    Console.SetOut(ConsOut); 
    //    while (true)
    //    {
    //        RunIbit();
    //    }
    //}

    //public static IWebHost BuildWebHost(string[] args)
    //{
    //    var config = new ConfigurationBuilder()
    //        .SetBasePath(Directory.GetCurrentDirectory())
    //        .AddJsonFile("appsettings.json", optional: true)
    //        .AddCommandLine(args)
    //        .Build();

    //    var host = new WebHostBuilder()
    //        .UseKestrel()
    //        .UseConfiguration(config)
    //        .UseStartup()
    //        .UseIISIntegration()
    //       .Build();

    //    return host;
    //}

    public static void RunIbit()
    {
        IServiceCollection services = new ServiceCollection();
        Startup startup = new Startup();
        var serviceProvider = startup.ConfigureServices(services);
        IWindsorContainer container = serviceProvider.GetService();
        var configuration = container.Resolve();
        var cron = configuration[Enums.ExecutionTime];
        var _logger = container.Resolve();
        var isRunning = false;
        Execute(cron); 
        while (true)
        {
            if (!isRunning)
            {
                isRunning = true;
            }
        }
    }

    public static async void Execute(string cron)
    {
        NameValueCollection props = new NameValueCollection
        {
            { "quartz.serializer.type", "binary" }
        };
        StdSchedulerFactory factory = new StdSchedulerFactory(props);

        var sched = await factory.GetScheduler();
        await sched.Start();

        var job = JobBuilder.Create()
            .Build();

        var trigger = TriggerBuilder.Create()
            .WithCronSchedule(cron)
            .ForJob(job.Key)
            .Build();
        await sched.ScheduleJob(job, trigger);
    }
}

[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class Job : IJob
{
    public Task Execute(IJobExecutionContext context)
    {
        var key = context.JobDetail.Key;
        try
        {
            var t = new Task(() =>
            {
                process.MainProcess();
            });
            return Task.CompletedTask;
        }
        catch (Exception e)
        {
        }
        return null;
    }
}
Posted
Updated 31-Jul-18 9:16am
v2

1 solution

Are you using version 3.x of Quartz? If so then take a look at this short discussion on how to run it on .NET Core: https://stackoverflow.com/questions/41821568/how-to-start-quartz-in-asp-net-core

The idea is to call your method for starting the scheduler before calling host.Run();
 
Share this answer
 
Comments
Member 12885549 31-Jul-18 15:33pm    
I seriously love you man! I spent ages and the solution was so easy! Thank you!
Vincent Maverick Durano 31-Jul-18 15:39pm    
Welcome! glad it worked for you.

PS: I actually used Quartz.net too, but never get the chance to try it with .NET Core yet. ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900