为什么HttpContext.Current空?

编程入门 行业动态 更新时间:2024-10-17 05:28:11
本文介绍了为什么HttpContext.Current空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我的所有应用程序中使用的值;我设置在的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空?

本文发布于:2023-11-13 15:02:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HttpContext   Current

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!