51

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();

  }

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();
    }
}

I want to get this value in a class.(smsjob)

   public class SmsJob : IJob 
   {  
      public virtual void Execute(IJobExecutionContext context)
      {
          HttpContext.Current.Application["Setting"]; 
      }
   }

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

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

Lex Li
  • 52,595
  • 8
  • 102
  • 129
ar.gorgin
  • 4,141
  • 8
  • 52
  • 89

3 Answers3

101

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".

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.

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

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.

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

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

instead of HttpContext.Current.

Lex Li
  • 52,595
  • 8
  • 102
  • 129
  • 17
    Also, have in mind that when using the keywords `async/await`, lines that follow an `await` are not neccessarily the same thread that handles the incoming request. Reference `HttpContext.Current` before your first `await` to be sure to avoid a `NullReferenceException` (I have sen functions where it works, other where it doesn't, so being consistent is key) – Tewr Mar 18 '14 at 14:42
  • @Lex Li But won't storing Session in a Singleton class have all Users to share same session – Arjuna Jan 12 '15 at 07:29
  • Is there any solution Lex? I'd like to access current user in a class library. – Akbari Dec 14 '15 at 07:26
  • 1
    @Lex Li what is your suggestion? – NinjaOnSafari May 30 '16 at 09:51
  • 3
    @Lex Li Be more to the point. You use unnecessarily colorful language and it's obscuring your answer. – Timothy Gonzalez Jan 04 '18 at 05:52
7

try to implement Application_AuthenticateRequest instead of Application_Start.

this method has an instance for HttpContext.Current, unlike Application_Start (which fires very soon in app lifecycle, soon enough to not hold a HttpContext.Current object yet).

hope that helps.

geevee
  • 5,201
  • 4
  • 28
  • 48
  • Thanks, but when i use this code in another class i get value, but in a this class `HttpContext.Current` is null. – ar.gorgin Oct 22 '13 at 05:48
  • 1
    as i said, it all is being called from `Application_Start`. move it under `Application_AuthenticateRequest` in global.asax and it will work! – geevee Oct 22 '13 at 05:59
  • 'Object reference not set to an instance of an object.' for `HttpContext.Current` – ar.gorgin Oct 22 '13 at 06:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39711/discussion-between-gal-v-and-ar-gorgin) – geevee Oct 22 '13 at 06:06
7

In IIS7 with integrated mode, Current is not available in Application_Start. There is a similar thread here.

Community
  • 1
  • 1
Tasos K.
  • 7,669
  • 7
  • 38
  • 57