title: WCF + TransactionScopeRequired + async + throw fault => ProtocolException "transaction under which method was executing was asynchronously aborted"
body: <p>Calling a WCF operation marked with "TransactionScopeRequired = true" on a service that uses an asynchronous host implementation (in my case using Tasks) and throws an exception asynchronously (i.e. from a continuation), always seems to result in the following exception (instead of the exception I threw) being received on the client:</p>
  
  <pre><code>System.AggregateException: One or more errors occurred. ---&gt; System.ServiceModel.ProtocolException: The transaction under which this method call was executing was asynchronously aborted.
     at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
     at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc&amp; rpc)
     at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
     at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.&lt;&gt;c__DisplayClass6_0.&lt;CreateTask&gt;b__0(IAsyncResult asyncResult)
     at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
     --- End of inner exception stack trace ---
     at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
     at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
     at System.Threading.Tasks.Task.Wait()
     at WcfTransactionScopeRequiredAsync.Program.Main(String[] args) in D:\\WcfTransactionScopeRequiredAsync\\WcfTransactionScopeRequiredAsync.Client\\Program.cs:line 19
  ---&gt; (Inner Exception #0) System.ServiceModel.ProtocolException: The transaction under which this method call was executing was asynchronously aborted.
     at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
     at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc&amp; rpc)
     at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
     at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.&lt;&gt;c__DisplayClass6_0.&lt;CreateTask&gt;b__0(IAsyncResult asyncResult)
     at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)&lt;---
  </code></pre>
  
  <p>Service contract:</p>
  
  <pre><code>[ServiceContract]
  public interface IService
  {
      [FaultContract(typeof(SomeFault))]
      [TransactionFlow(TransactionFlowOption.Allowed)]
      [OperationContract]
      Task DoItAsync();
  }
  </code></pre>
  
  <p>Service host implementation:</p>
  
  <pre><code>[OperationBehavior(TransactionScopeRequired = true)]
  public async Task DoItAsync()
  {
      using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled))
      {
          await Task.Delay(1000).ConfigureAwait(continueOnCapturedContext: false);
          throw new FaultException&lt;SomeFault&gt;(new SomeFault());
          tx.Complete();
      }
  }
  </code></pre>
  
  <p>The client is synchronous:</p>
  
  <pre><code>service.DoItAsync().Wait();
  </code></pre>
  
  <p>The client does not flow a transaction in this case (so this is definitely not a distributed transaction (verified by checking Transaction.Current.TransactionInformation.DistributedIdentifier).</p>
  
  <p>This code was tested with .NET framework 4.7.2.</p>
  
  <p>My research so far suggests that this ProtocolException is thrown by the WCF pipeline on the service host (also evident from the stack trace in the Exception) and would supposedly be meant to deal with distributed transactions being aborted outside of the host's control (i.e when the DTC issues the abort). Although this appears to make some sense, this is clearly not the case here.</p>
  
  <p>Is there any way to avoid this ProtocolException while still using an async implementation on the host?</p>