为什么 HttpContext.Current 为空? [英] Why is HttpContext.Current null?

查看:91
本文介绍了为什么 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.

如果您需要将某些信息向下传递到业务逻辑层,请将其作为参数传递给相关方法.不要让你的业务逻辑层访问诸如 HttpContextApplication["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

而不是 HttpContext.Current.

这篇关于为什么 HttpContext.Current 为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆