title: When not to call FlushAsync on IAsyncCollector in Azure Functions ServiceBus binding
body: <p>I have a range of Azure Function apps all outputting messages to Azure Service Bus using the <code>IAsyncCollector&lt;Message&gt;</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>public async Task Run([ServiceBus(...)] IAsyncCollector&lt;Message&gt; messages)
  {
      ...
      await messages.AddAsync(msg);
  }
  </code></pre>
  <p>I'm getting errors logged from time to time looking like this:</p>
  <pre class="lang-cs prettyprint-override"><code>Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Function
   ---&gt; Microsoft.Azure.ServiceBus.ServiceBusTimeoutException: The operation did not complete within the allocated time 00:00:59.9999536 for object message.Reference: ..., 7/13/2020 2:46:24 PM
   ---&gt; System.TimeoutException: The operation did not complete within the allocated time 00:00:59.9999536 for object message.
     at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result)
     at Microsoft.Azure.Amqp.SendingAmqpLink.EndSendMessage(IAsyncResult result)
     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 Microsoft.Azure.ServiceBus.Core.MessageSender.OnSendAsync(IList`1 messageList)
     --- End of inner exception stack trace ---
     at Microsoft.Azure.ServiceBus.Core.MessageSender.OnSendAsync(IList`1 messageList)
     at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
     at Microsoft.Azure.ServiceBus.RetryPolicy.RunOperation(Func`1 operation, TimeSpan operationTimeout)
     at Microsoft.Azure.ServiceBus.Core.MessageSender.SendAsync(IList`1 messageList)
     at Microsoft.Azure.WebJobs.ServiceBus.Bindings.MessageSenderExtensions.SendAndCreateEntityIfNotExists(MessageSender sender, Message message, Guid functionInstanceId, EntityType entityType, CancellationToken cancellationToken)
     at My.Function.Run(String mySbMsg, IAsyncCollector`1 messages)
  </code></pre>
  <p>I'm having a bit of a hard time figuring out when in the pipeline this happens. But I have recently learned about the <code>FlushAsync</code> method:</p>
  <pre class="lang-cs prettyprint-override"><code>await messages.AddAsync(msg);
  await messages.FlushAsync();
  </code></pre>
  <p>My question is the following. Why would I ever NOT include a call to <code>FlushAsync</code> in my function? Getting the timeout exception in my own code will make it possible to retry, do better exception logging, and more. Any downsides of flushing manually like this within the function code?</p>