Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to gain access to an HttpContext object in a Quartz.NET job?

Is there any way to gain access to the HttpContext object from a Quartz.NET job? HttpContext.Current and the likes do not seem to work with Quartz.NET jobs.

like image 342
jon333 Avatar asked Mar 22 '23 17:03

jon333


2 Answers

Yes there is a way.
Just set HttpContext.Current to JobDataMap when instantiating new scheduler(probably in Application_Start event in Global.asax) like this:

jobDetail.JobDataMap["context"] = HttpContext.Current;

Then access it in Execute method like this:

HttpContext context = context.JobDetail.JobDataMap["context"] as HttpContext;
like image 146
Jalali Shakib Avatar answered Apr 27 '23 23:04

Jalali Shakib


In short, no.

Jobs are ran on different threads that do not know about a HTTP request that has happened at some point. The job might run after the request was processed and thus the context would be invalid anyway.

With frameworks like ASP.NET MVC you can do some things without actual context, like generating route urls etc but request and response (pretty much the context) are not available.

You need to partition the responsibilities so that Quartz jobs can work autonomously.

like image 26
Marko Lahma Avatar answered Apr 28 '23 00:04

Marko Lahma