问题
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.
回答1:
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;
回答2:
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.
来源:https://stackoverflow.com/questions/19909498/is-there-any-way-to-gain-access-to-an-httpcontext-object-in-a-quartz-net-job