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();
}
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;
}
}