title: How to dispose TransactionScope in cancelable async/await?
body: <p>I'm trying to use the new async/await feature to asynchronously work with a DB. As some of the requests can be lengthy, I want to be able to cancel them. The issue I'm running into is that <code>TransactionScope</code> apparently has a thread affinity, and it seems that when canceling the task, its <code>Dispose()</code> gets ran on a wrong thread.</p>
  
  <p>Specifically, when calling <code>.TestTx()</code> I get the following <code>AggregateException</code> containing <code>InvalidOperationException</code> on <code>task.Wait ()</code>:</p>
  
  <pre><code>"A TransactionScope must be disposed on the same thread that it was created."
  </code></pre>
  
  <p>Here's the code:</p>
  
  <pre><code>public void TestTx () {
      var cancellation = new CancellationTokenSource ();
      var task = TestTxAsync ( cancellation.Token );
      cancellation.Cancel ();
      task.Wait ();
  }
  
  private async Task TestTxAsync ( CancellationToken cancellationToken ) {
      using ( var scope = new TransactionScope () ) {
          using ( var connection = new SqlConnection ( m_ConnectionString ) ) {
              await connection.OpenAsync ( cancellationToken );
              //using ( var command = new SqlCommand ( ... , connection ) ) {
              //  await command.ExecuteReaderAsync ();
              //  ...
              //}
          }
      }
  }
  </code></pre>
  
  <p>UPDATED: the commented out part is to show there's something to be done — asynchronously — with the connection once it's open, but that code is not required to reproduce the issue.</p>