title: How to schedule a job using FluentScheduler library with Web Api?
body: <p>I am unable to get FluentScheduler working in .Net Framework 4.5.2 Web api. Few days ago, I asked a similar question about scheduling through Console application and could get it to work with help but unfortunately facing issues with Web Api now. Below is the code.</p>
  
  <pre><code>    [HttpPost]
      [Route("Schedule")]
      public IHttpActionResult Schedule([FromBody] SchedulerModel schedulerModel)
      {
          var registry = new Registry();
          registry.Schedule&lt;MyJob&gt;().ToRunNow();
          JobManager.Initialize(registry);
          JobManager.StopAndBlock();
          return Json(new { success = true, message = "Scheduled!" });
      }
  </code></pre>
  
  <p>Below is the job I want to schedule which for now is just writing text to a file</p>
  
  <pre><code>public class SampleJob: IJob, IRegisteredObject
  {
      private readonly object _lock = new object();
      private bool _shuttingDown;
  
      public SampleJob()
      {
          HostingEnvironment.RegisterObject(this);
      }
  
      public void Execute()
      {
          lock (_lock)
          {
              if (_shuttingDown)
                  return;
            //Schedule writing to a text file
             WriteToFile();
          }
      }
  
      public void WriteToFile()
      {
          string text = "Random text";
          File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
      }
  
      public void Stop(bool immediate)
      {
          lock (_lock)
          {
              _shuttingDown = true;
          }            
          HostingEnvironment.UnregisterObject(this);
      }
  </code></pre>