title: How to convert Action to Func<Task> without running the Task?
body: <p>I have code that works precisely as desired. However, our corporate build server rejects any check-in that has a compiler warning.</p>
  <p>The following warning is (as expected) displayed for the Action constructor with the Action to Func conversions, since I am not using an await statement.</p>
  <blockquote>
  <p>This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.</p>
  </blockquote>
  <pre><code>public class TransactionOperation
  {
      private readonly Func&lt;Task&gt; _operation;
      private readonly Func&lt;Task&gt; _rollback;
  
      public OperationStatus Status { get; private set; }
  
      public TransactionOperation(Func&lt;Task&gt; operation, Func&lt;Task&gt; rollback)
      {
          _operation = operation;
          _rollback = rollback;
          Status = OperationStatus.NotStarted;
      }
  
      public TransactionOperation(Action operation, Action rollback)
      {
          _operation = async () =&gt; operation.Invoke();
          _rollback = async () =&gt; rollback.Invoke();
          Status = OperationStatus.NotStarted;
      }
  
      public async Task Invoke()
      {
          try
          {
              Status = OperationStatus.InProcess;
              await _operation.Invoke();
              Status = OperationStatus.Completed;
          }
          catch (Exception ex)
          {
              //...
          }
      }
  }
  </code></pre>
  <p>What is a correct way to rewrite that code so that the Action is correctly converted to Func without being executed yet or creating a new thread (i.e. await Task.Run())?</p>
  <hr />
  <p><strong>Update - Proposed Answer #1</strong></p>
  <blockquote>
  <p>_operation = () =&gt; new Task(operation.Invoke);</p>
  <p>_rollback = () =&gt; new Task(rollback.Invoke);</p>
  </blockquote>
  <p>I tried this before. It causes this unit test to never return.</p>
  <pre><code>[TestMethod, TestCategory(&quot;Unit Test&quot;)]
  public async Task Transaction_MultiStepTransactionExceptionOnFourthAction_CorrectActionsRolledBack()
  {
      var operation = new TransactionOperation(PerformAction, () =&gt; RollbackOperation(1));
      var operation2 = new TransactionOperation(PerformAction, () =&gt; RollbackOperation(2));
      var operation3 = new TransactionOperation(PerformAction, () =&gt; RollbackOperation(3));
      var operation4 = new TransactionOperation(ExceptionAction, () =&gt; RollbackOperation(4));
      var operation5 = new TransactionOperation(PerformAction, () =&gt; RollbackOperation(5));
      var transaction = new Transaction(new[] { operation, operation2, operation3, operation4, operation5 });
  
      await IgnoreExceptions(transaction.ExecuteAsync);
  
      AssertActionsPerformedThrough(4);
      AssertActionsRolledBackThrough(4);
  }
  </code></pre>
  <hr />
  <p><strong>Update - Accepted Answer</strong></p>
  <pre><code>private async Task ConvertToTask(Action action)
  {
      action.Invoke();
      await Task.FromResult(true);
  }
  </code></pre>
  <p>Here's the updated Action constructor:</p>
  <pre><code>public TransactionOperation(Action operation, Action rollback)
  {
      _operation = () =&gt; ConvertToTask(operation);
      _rollback = () =&gt; ConvertToTask(rollback);
      Status = OperationStatus.NotStarted;
  }
  </code></pre>