为什么 HttpContext.Current 为空?

编程入门 行业动态 更新时间:2024-10-18 01:27:39
本文介绍了为什么 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 为空,为什么 HttpContext.Current 为空?

but my problem is : HttpContext.Current is null, why is HttpContext.Current null?

当我在页面的另一个类中使用此代码时它可以工作,但在这个类中我收到错误.

When i use this code in another class of a page it works, but in this class I get the error.

推荐答案

显然 HttpContext.Current 不是 null 仅当您在处理传入请求的线程中访问它时.这就是当我在页面的另一类中使用此代码时"的原因.

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 the scheduling related class because relevant code is not executed on a valid thread, but a background thread, which has no HTTP context associated with.

总的来说,不要使用 Application["Setting"] 来存储全局内容,因为它们不是您发现的全局内容.

Overall, don't use Application["Setting"] to store global stuffs, as they are not global as you discovered.

如果您需要将某些信息向下传递到业务逻辑层,请将其作为参数传递给相关方法.不要让你的业务逻辑层访问诸如 HttpContext 或 Application["Settings"] 之类的东西,因为这违反了隔离和解耦的原则.

If you need to pass certain information down to business logic layer, pass as arguments to the related methods. Don't let your business logic layer access things like HttpContext or Application["Settings"], as that violates the principles of isolation and decoupling.

更新:由于引入了async/await,此类问题发生的频率更高,因此您可以考虑以下提示,

Update: Due to the introduction of async/await it is more often that such issues happen, so you might consider the following tip,

通常,您应该只在少数情况下(例如在 HTTP 模块中)调用 HttpContext.Current.在所有其他情况下,您应该使用

In general, you should only call HttpContext.Current in only a few scenarios (within an HTTP module for example). In all other cases, you should use

  • Page.Context docs.microsoft/en-us/dotnet/api/system.web.ui.page.context?view=netframework-4.7.2
  • Controller.HttpContext docs.microsoft/en-us/dotnet/api/system.web.mvc.controller.httpcontext?view=aspnet-mvc-5.2

而不是 HttpContext.Current.

更多推荐

为什么 HttpContext.Current 为空?

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

发布评论

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

>www.elefans.com

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