为什么HttpContext.Current空? [英] Why is HttpContext.Current null?
问题描述
我有我的所有应用程序中使用的值;我设置在的Application_Start
I have a value that I use in all the application; I set this in application_start
void Application_Start(object sender, EventArgs e)
{
Dictionary<int, IList<string>> Panels = new Dictionary<int, IList<string>>();
List<clsPanelSetting> setting = clsPanelSettingFactory.GetAll();
foreach (clsPanelSetting panel in setting)
{
Panels.Add(panel.AdminId, new List<string>() { panel.Phone,panel.UserName,panel.Password});
}
Application["Setting"] = Panels;
SmsSchedule we = new SmsSchedule();
we.Run();
}
和在SmsSchedule
and in SmsSchedule
public class SmsSchedule : ISchedule
{
public void Run()
{
DateTimeOffset startTime = DateBuilder.FutureDate(2, IntervalUnit.Second);
IJobDetail job = JobBuilder.Create<SmsJob>()
.WithIdentity("job1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1")
.StartAt(startTime)
.WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever())
.Build();
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc = sf.GetScheduler();
sc.ScheduleJob(job, trigger);
sc.Start();
}
}
我想在一个类此值。(smsjob)
I want to get this value in a class.(smsjob)
public class SmsJob : IJob
{
public virtual void Execute(IJobExecutionContext context)
{
HttpContext.Current.Application["Setting"];
}
}
但我的问题是:HttpContext.Current为null,为什么HttpContext.Current空
but my problem is : HttpContext.Current is null, why is HttpContext.Current null?
编辑:
当我用这个code另一类它的工作原理页面,但在这个类我得到的错误。
When i use this code in another class of a page it works, but in this class I get the error.
推荐答案
显然 HttpContext.Current
不是空
只有当你访问它在处理传入的请求的线程。这就是为什么它的工作原理时,我用这个code另一个类的网页。
Clearly HttpContext.Current
is not null
only if you access it in a thread that handles incoming requests. That's why it works "when i use this code in another class of a page".
它不会在你的调度相关工作类,只是因为在一个有效的线程未执行类,但在后台线程它没有HTTP上下文的。
It won't work in your scheduling related class simply because that class is not executed on a valid thread, but a background thread which has no HTTP context at all.
不要使用应用[设置]
来存储全局的东西,因为它们不是全球都为你刚刚得知。
Don't use Application["Setting"]
to store global stuffs, as they are not global at all as you just learned.
这篇关于为什么HttpContext.Current空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!