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

左心房为你撑大大i 提交于 2019-12-10 08:34:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!