title: Testing an asynchronous WCF service using MSTest
body: <p>I've implemented a WCF service using the async\await method in C#. The service operation contract is as follows:</p>
  
  <pre><code>[OperationContract]
  Task&lt;ResponseMessage&lt;ProcessMultipleTransactionsResponse&gt;&gt; ProcessMultipleTransactionsAsync(RequestMessage&lt;ProcessMultipleTransactionsRequest&gt; msgIn);
  </code></pre>
  
  <p>The method signature for the implementation is:</p>
  
  <pre><code>public async Task&lt;ResponseMessage&lt;ProcessMultipleTransactionsResponse&gt;&gt; ProcessMultipleTransactionsAsync(RequestMessage&lt;ProcessMultipleTransactionsRequest&gt; msgIn)
  </code></pre>
  
  <p>I'm trying to test this service using a unit test in VS 2012. After building a proxy I consume the method using:</p>
  
  <pre><code>var actual = await target.ProcessMultipleTransactionsAsync(request);
  </code></pre>
  
  <p>The test method has the async keyword with a Task as the return type. When executing the test I'm getting the following error:</p>
  
  <p>Test method Services.Implementations.UnitTests.CoreBanking.CoreBankingServiceTest.ProcessMultipleTransactions_JMD_JMD_ACHTransactions_Test threw exception: 
  System.ArgumentException: <strong>An incorrect IAsyncResult was provided to an 'End' method. The IAsyncResult object passed to 'End' must be the one returned from the matching 'Begin' or passed to the callback provided to 'Begin'.</strong>
  Parameter name: result</p>
  
  <p>Result StackTrace:</p>
  
  <pre class="lang-none prettyprint-override"><code>at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
  at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
  at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
  at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.&lt;&gt;c__DisplayClass2`1.&lt;CreateGenericTask&gt;b__3(IAsyncResult asyncResult)
  at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at Services.Implementations.UnitTests.CoreBanking.CoreBankingServiceTest.&lt;ProcessMultipleTransactions_JMD_JMD_ACHTransactions_Test&gt;d__10.MoveNext() in c:\...\CoreBankingServiceTest.cs:line 5716
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
  </code></pre>
  
  <p>Any ideas what may be causing this?</p>