我在应用程序中使用一个值; 我在 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中
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)
public class SmsJob : IJob
{
public virtual void Execute(IJobExecutionContext context)
{
HttpContext.Current.Application["Setting"];
}
}
但我的问题是:HttpContext.Current为空,为什么HttpContext.Current为空?
编辑: 当我在页面的另一个类中使用此代码时,它可以工作,但在这个类中我会出现错误。
async/await
关键字时,跟在await
后面的代码行不一定是处理传入请求的同一线程。在第一个await
之前引用HttpContext.Current
,以确保避免出现NullReferenceException
(我见过有些函数可以正常工作,有些则不行,因此保持一致性非常关键)。 - Tewr