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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With