title: How to support async methods in a TransactionScope with Microsoft.Bcl.Async in .NET 4.0?
body: <p>I have a method similar to:</p>
  
  <pre><code>public async Task SaveItemsAsync(IEnumerable&lt;MyItem&gt; items)
  {
      using (var ts = new TransactionScope())
      {
          foreach (var item in items)
          {
              await _repository.SaveItemAsync(item);
          }
  
          await _repository.DoSomethingElse();
  
          ts.Complete();
      }
  }
  </code></pre>
  
  <p>This of course has issues because <code>TransactionScope</code> doesn't play nice with async/await.</p>
  
  <p>It fails with an <code>InvalidOperationException</code> with the message:</p>
  
  <blockquote>
    <p>"A TransactionScope must be disposed on the same thread that it was created."</p>
  </blockquote>
  
  <p>I read about <a href="http://msdn.microsoft.com/en-us/library/system.transactions.transactionscopeasyncflowoption.aspx" rel="noreferrer"><code>TransactionScopeAsyncFlowOption</code></a> in <a href="https://stackoverflow.com/a/17527759/634824">this answer</a>, which appears to be exactly what I need.</p>
  
  <p>However, for this particular project, I have a hard requirement to support .Net 4.0 and cannot upgrade to 4.5 or 4.5.1.  Thus the async/await behavior in my project is provided by the <a href="https://www.nuget.org/packages/Microsoft.Bcl.Async/" rel="noreferrer">Microsoft.Bcl.Async NuGet Package</a>.</p>
  
  <p>I can't seem to find <code>TransactionScopeAsyncFlowOption</code> in this or any other <a href="http://msdn.microsoft.com/en-us/library/dn151288.aspx" rel="noreferrer">OOB package</a>.  Am I just missing it somewhere?</p>
  
  <p>If it is not available, is there an alternative for achieving the same result?  That is - I would like the transaction scope to properly complete or rollback, despite crossing threads with continuations.</p>
  
  <p>I added <code>DoSomethingElse</code> in the example above to illustrate that there may be multiple calls to make within the transaction scope, so simply passing all items to the database in one call is not a viable option.</p>
  
  <p>In case it matters, the repository uses direct ADO.Net (<code>SqlConnection</code>, <code>SqlCommand</code>, etc) to write to a SQL Server.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>I thought I had a solution which involved taking System.Transactions.dll from .Net 4.5.1 and including it in my project.  However, I found that this worked only on my dev box because it already had 4.5.1 installed.  It did not work when deploying to a machine with only .Net 4.0.  It just gave a <code>MissingMethodException</code>.  I'm looking for a solution that will work on a .Net 4.0 installation.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I originally asked this question in July 2014.  .NET Framework 4.0, 4.5, and 4.5.1 reached <a href="https://support.microsoft.com/en-us/help/17455/lifecycle-faq-net-framework" rel="noreferrer">end of life</a> in January 2016.  The question thus is no longer applicable and is here only for historical reference.</p>