title: using Microsoft.ServiceBus.Messaging.MessageSession Many messages Complete(), Abort(), Etc
body: <p>I am having a problem closing out the MessageSession when there are more than one messages to be received... I wait for all of the sequence to come in and then I try and "Complete()" all of the messages in the session. I have tried lots of different ways from iterating through the messages received and "Complete()"-ing them, "Close()"-ing the session, "Completing()"-ing the session, I cannot find the winning formula for the life of me. Microsoft's documentation is not very helpful either. It certainly doesn't provide a decent example. The following is the code as I have it in it's most recent iteration </p>
  
  <p>public   QueueMessageBody ReceiveResponse(string sessionId)
          {</p>
  
  <pre><code>        try
          {
              MessagingFactory _messagingFactory;
              _messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
  
              var receiver = _messagingFactory.CreateQueueClient(ResponseQueue, ReceiveMode.PeekLock);
              var messageBodyStream = new MemoryStream();
  
              while (true)
              {
                  Console.WriteLine("Waiting for new message");
                  using (var scope = new TransactionScope())
                  {
                      var receiverSession = receiver.AcceptMessageSession(sessionId);
                      messageBodyStream = new MemoryStream();
  
                      var keepPolling = true;
                      var isFirstMessage = true;
                      var expectedNoMessages = 0;
                      BrokeredMessage[] messages = null;
                      var messagesReceived = 0;
                      while (keepPolling)
                      {
                          var message = receiverSession.Receive(TimeSpan.FromSeconds(10));
                          if (message == null)
                              continue;
  
                          if (isFirstMessage)
                          {
                              Console.WriteLine("Receiving first message");
                              expectedNoMessages = (int)message.Properties["TotalMessages"];
                              messages = new BrokeredMessage[expectedNoMessages];
                              isFirstMessage = false;
                          }
  
                          var messageNo = (int)message.Properties["MessageNo"];
                          var messageIndex = messageNo - 1;
                          Console.WriteLine(string.Format("Receiving message {0}", messageNo));
                          messages[messageIndex] = message;
                          messagesReceived++;
  
                          if (messagesReceived == expectedNoMessages)
                              keepPolling = false;
                      }
  
  
  
  
  
                      //Rebuild Object
                      var messageBodyBuilder = new ChunkedMessageBuilder().ReconstructMessageBody(messages);
                      var o = SerializationHelper.Deserialize&lt;QueueMessageBody&gt;(messageBodyBuilder);
  
                      var completeTasks = new List&lt;Task&gt;();
                      if (messages.Count()&lt; 3)
                      {
                          foreach (var message in messages)
                              completeTasks.Add(receiverSession.CompleteAsync(message.LockToken));
                          Task.WaitAll(completeTasks.ToArray());
                      }else
                      {
                          receiverSession.Close();
                      }
  
  
                      return o;
  
  
                  }
              }
  
  
  
          }
          catch(Exception ex)
          {
             System.Diagnostics.Debug.Print(ex.Message);
              return null;
          }
  
      }
  
  
    public   QueueMessageBody ReceiveResponse(string sessionId)
      {
  
          try
          {
              MessagingFactory _messagingFactory;
              _messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);
  
              var receiver = _messagingFactory.CreateQueueClient(ResponseQueue, ReceiveMode.PeekLock);
              var messageBodyStream = new MemoryStream();
  
              while (true)
              {
                  Console.WriteLine("Waiting for new message");
                  using (var scope = new TransactionScope())
                  {
                      var receiverSession = receiver.AcceptMessageSession(sessionId);
                      messageBodyStream = new MemoryStream();
  
                      var keepPolling = true;
                      var isFirstMessage = true;
                      var expectedNoMessages = 0;
                      BrokeredMessage[] messages = null;
                      var messagesReceived = 0;
                      while (keepPolling)
                      {
                          var message = receiverSession.Receive(TimeSpan.FromSeconds(10));
                          if (message == null)
                              continue;
  
                          if (isFirstMessage)
                          {
                              Console.WriteLine("Receiving first message");
                              expectedNoMessages = (int)message.Properties["TotalMessages"];
                              messages = new BrokeredMessage[expectedNoMessages];
                              isFirstMessage = false;
                          }
  
                          var messageNo = (int)message.Properties["MessageNo"];
                          var messageIndex = messageNo - 1;
                          Console.WriteLine(string.Format("Receiving message {0}", messageNo));
                          messages[messageIndex] = message;
                          messagesReceived++;
  
                          if (messagesReceived == expectedNoMessages)
                              keepPolling = false;
                      }
  
  
  
  
  
                      //Rebuild Object
                      var messageBodyBuilder = new ChunkedMessageBuilder().ReconstructMessageBody(messages);
                      var o = SerializationHelper.Deserialize&lt;QueueMessageBody&gt;(messageBodyBuilder);
  
                      var completeTasks = new List&lt;Task&gt;();
                      if (messages.Count()&lt; 3)
                      {
                          foreach (var message in messages)
                              completeTasks.Add(receiverSession.CompleteAsync(message.LockToken));
                          Task.WaitAll(completeTasks.ToArray());
                      }else
                      {
                          receiverSession.Close();
                      }
  
  
                      return o;
  
  
                  }
              }
  
  
  
          }
          catch(Exception ex)
          {
             System.Diagnostics.Debug.Print(ex.Message);
              return null;
          }
  
      }
  </code></pre>
  
  <p>And the queue is configured as follows.</p>
  
  <p><a href="https://i.stack.imgur.com/s1fOE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s1fOE.png" alt="The Queue configuration."></a></p>
  
  <p>I am able to receive the messages and complete the workflow on them, the problem is they just stay there in the queue. I cannot complete them at</p>