我有Quartz.NET作业,该作业每天在同一小时发生,并且运行Process
。之后,Web界面崩溃。
看起来是这样:
class Program
{
private static IScheduler _scheduler;
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
StartScheduler(cron);
host.Run();
}
private static void StartScheduler()
{
var props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
_scheduler = factory.GetScheduler().Result;
_scheduler.Start().Wait();
var job = JobBuilder.Create<Job>()
.Build();
var trigger = TriggerBuilder.Create()
.WithCronSchedule("0 0 7 * * ?")
.ForJob(job.Key)
.Build();
_scheduler.ScheduleJob(job, trigger).Wait();
}
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class Job : IJob
{
public Task Execute(IJobExecutionContext context)
{
var key = context.JobDetail.Key;
var startup = new Startup();
var serviceProvider = startup.ConfigureServices(new ServiceCollection());
var container = serviceProvider.GetService<IWindsorContainer>();
var process = container.Resolve<TestProcess.TestProcess>();
try
{
var t = new Task(() =>
{
process.MainProcess();
});
t.Start();
t.Wait();
return Task.CompletedTask;
}
catch (Exception e)
{}
finally
{
container.Release(process);
}
return null;
}
}
}
为什么会这样?