在.Net Core Web应用程序'Microsoft.NET.Sdk.Web'和'PreserveCompilationContext'上执行Backgroundjob之后

时间:2018-08-02 04:45:01

标签: asp.net .net asp.net-core quartz.net-3.0

我有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;
        }
    }
}

这是错误: enter image description here

为什么会这样?

0 个答案:

没有答案