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.
Session will never be null if you have any variable in the Session. HttpContext. Current. Session represents a collection of all the session values that you are storing in the session.
Your test is not flawed and HttpContext. Current should not be null after the await because in ASP.NET Web API when you await, this will ensure that the code that follows this await is passed the correct HttpContext that was present before the await.
Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests.
HttpContext is an object that wraps all http related information into one place. HttpContext. Current is a context that has been created during the active request. Here is the list of some data that you can obtain from it.
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
Page.Context
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.context?view=netframework-4.7.2 Controller.HttpContext
https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.httpcontext?view=aspnet-mvc-5.2 instead of HttpContext.Current
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With