Hi I am developing an automated file generator which generates file on daily basis for this purpose, I am using Hangfire background worker but the problem is when it call a method of file generator HttpContext giving me null object error this mean I cannot
acesss HttpContext when there is no controller request
Here is my complete code of file generator
In Startup class
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("HangFireCon");
app.UseHangfireDashboard();
//app.UseHangfire(config=> {
// config.UseServer(1)
//});
var options = new BackgroundJobServerOptions { WorkerCount = 1 };
RecurringJob.AddOrUpdate(() => home.GenerateFile(), Cron.Daily);
app.UseHangfireServer(options);
}
but the problem is when it call a method of file generator HttpContext giving me null object error this mean I cannot acesss HttpContext when there is no controller request
Correct. Your approach will not work. The scheduled task should save the file using System.IO.
Keep in mind that an HTTP request/response requires a client connection. If there's no connection with a client then there is no place to return an Http Response.
Member
25 Points
49 Posts
How to Access HttpContext in ScheduleJob related method
Aug 09, 2019 11:26 AM|AfaqRajput|LINK
Hi I am developing an automated file generator which generates file on daily basis for this purpose, I am using Hangfire background worker but the problem is when it call a method of file generator HttpContext giving me null object error this mean I cannot acesss HttpContext when there is no controller request
Here is my complete code of file generator
In Startup class
Home Controller
All-Star
53041 Points
23612 Posts
Re: How to Access HttpContext in ScheduleJob related method
Aug 09, 2019 11:34 AM|mgebhard|LINK
Correct. Your approach will not work. The scheduled task should save the file using System.IO.
Member
25 Points
49 Posts
Re: How to Access HttpContext in ScheduleJob related method
Aug 09, 2019 11:42 AM|AfaqRajput|LINK
can you please share the sample code here?
All-Star
53041 Points
23612 Posts
Re: How to Access HttpContext in ScheduleJob related method
Aug 09, 2019 11:46 AM|mgebhard|LINK
See the following which explains how to save a file.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file
Keep in mind that an HTTP request/response requires a client connection. If there's no connection with a client then there is no place to return an Http Response.