Cannot use Server.MapPath in code for a recurrring job

I am trying to use Server.MapPath inside the method _SendJobEmail_that gets executed every minute, but it isn’t working.

Question :Is there a way to get the full physical path in code below?

Schedule the job

    RecurringJob.AddOrUpdate(() => EmailJob.SendJobEmail(), Cron.Minutely());

Job Method

public void SendJobEmail()
 {
   string path = @"~/folder1/";
   path = System.Web.HTTPContext.Current.Server.MapPath(path);
   //some more code that has been omitted
}

Try use HostingEnvironment.MapPath

HangFire Do not support HTTPContext.Current due to platform independence hence it will always throw error.
I am not sure for working status of HostingEnvirement.Mappath.
Couple of option you have to get out from this issue.

  • Use System.IO to get files
  • Pass Physical File Path To Job.
  • Store somewhere Collection of FilePath ie DB, or XML file. so you can retrieve it later, and When you need to modify that path you can easily do that without affecting execution of JOB.

HostingEnvirement.MapPath should work just fine, as long as the code is running InProc with the website application

Thanks for the answer. I am assuming that Hangfire job will by default execute in-process. Is that correct?

Hangfire always runs as an in-process component in an ASP.Net application, even though it uses a separate thread pool to run and not the ASP.Net thread pool. Different thread pool does not mean it runs in a different process.