title: How to wrap a Hangfire cancellation token?
body: <p>I'm building up a .net core web app that requires background tasks to be run. To avoid using an external cron triggers we've decided to go with Hangfire; which is a beautiful package to use, does exactly whats needed then gets out of the way ;-)</p>
  
  <p>To keep things clean I'm trying to stick to Uncle Bob's Clean architecture principles and seperate my ApplicationCore from the Infrastructure as much as possible. As Hangfire is an implementation detail it should ideally sit in the Infrastructure project, alongside database access, message queues, etc. with an Interface in the ApplicationCore, that my domain can use.</p>
  
  <p>For the basic client running recurring and background jobs this has been fairly simple to do, and I've ended up with this as my <code>Interface</code></p>
  
  <pre><code>namespace ApplicationCore.Interfaces
  {
      using System;
      using System.Linq.Expressions;
      using System.Threading.Tasks;
  
      public interface IBackgroundJobClient
      {
          void AddOrUpdate&lt;T&gt;(
              string recurringJobId,
              Expression&lt;Func&lt;T, Task&gt;&gt; methodCall,
              string cronExpression);
  
           void RemoveIfExists(string recurringJobId);
      }
  }
  </code></pre>
  
  <p>Implementation is a simple wrapper for these methods which uses <code>RecurringJob</code></p>
  
  <pre><code>namespace Infrastructure.BackgroundJobs
  {
      using System;
      using System.Linq.Expressions;
      using System.Threading.Tasks;
      using Hangfire;
      using IBackgroundJobClient = ApplicationCore.Interfaces.IBackgroundJobClient;
  
      public class HangfireBackgroundJobClient : IBackgroundJobClient
      {
          public void AddOrUpdate&lt;T&gt;(
              string recurringJobId,
              Expression&lt;Func&lt;T, Task&gt;&gt; methodCall,
              string cronExpression)
          {
              RecurringJob.AddOrUpdate&lt;T&gt;(recurringJobId, methodCall, cronExpression);
          }
  
          public void RemoveIfExists(string recurringJobId)
          {
              RecurringJob.RemoveIfExists(recurringJobId);
          }
      }
  </code></pre>
  
  <p>The issue that I have is needing to setup a <code>RecurringJob</code> with a supplied cancellationToken. However, I can't see an easy way to do this without exposing the underlying <code>IJobCancellationToken</code> and <code>JobCancellationToken</code> objects in my ApplicationCore code...?</p>
  
  <p>What I've got at present is a wrapper for the <code>JobCancellationToken</code> in my Infrastructure.</p>
  
  <pre><code>namespace Infrastructure.BackgroundJobs
  {
      public class BackgroundJobCancellationToken : JobCancellationToken
      {
          public BackgroundJobCancellationToken(bool canceled): base(canceled)
          {
          }
      }
  }
  </code></pre>
  
  <p>An Interface in my ApplicationCore, which replicates the Hangfire one.</p>
  
  <pre><code>namespace ApplicationCore.Interfaces
  {
      using System.Threading;
  
      public interface IJobCancellationToken
      {
          CancellationToken ShutdownToken { get; }
  
          void ThrowIfCancellationRequested();
      }
  }
  </code></pre>
  
  <p>This is then used by the Method I want to execute as job, making use of the <code>cancellationToken.ShutdownToken</code> to pass into other methods requiring a cancellationToken.</p>
  
  <pre><code>public async Task GenerateSubmission(Guid SetupGuidId, IJobCancellationToken cancellationToken)
          {
              try
              {
                  // Sort out the entities that we'll need
                  var setup = await this.SetupRepository.GetByGuidIdAsync(SetupGuidId);
  
                  var forecast = await this.GetCurrentForecastForSetup(setup, DateTime.UtcNow, cancellationToken.ShutdownToken);
  
                  // Other Code
  
                  }
              catch (Exception e)
              {
                  Console.WriteLine(e);
                  throw;
              }
          }
  </code></pre>
  
  <p>Which in turn is enabled elsewhere by calling</p>
  
  <pre><code>    public async Task&lt;Setup&gt; EnableSetup(Setup setup)
      {
          setup.Enable();
  
          this.jobClient
              .AddOrUpdate&lt;IForecastService&gt;(
                  setup.GuidId.ToString(),
                  f =&gt; f.GenerateSubmission(setup.GuidId, null),
                  "45 */2 * * *");
  
          await this.setupRepository.UpdateAsync(setup);
  
          return setup;
      }
  </code></pre>
  
  <p>This should be done with DomainEvents and Handlers, but one step at a time :-)</p>
  
  <p>Is there a cleaner, better, easier way of doing this without taken a direct dependency on Hangfire in my ApplicationCore?</p>
  
  <p>If the above setup works, I'll leave a comment on this question.</p>