title: How do I gracefully fail a Service Bus triggered Azure Function
body: <p>I want to use Azure Functions to call REST endpoints based on Queue messages.
  <a href="https://azure.microsoft.com/en-us/documentation/articles/functions-bindings-service-bus/" rel="nofollow">Documentation</a> tells me ...</p>
  
  <blockquote>
    <p>The Functions runtime receives a message in PeekLock mode and calls Complete on the message if the function finishes successfully, or calls Abandon if the function fails. </p>
  </blockquote>
  
  <p>Hence I try to fail the function by throwing an exception for the host to abandon the message when the REST call fails. </p>
  
  <pre><code>using System;
  using System.Text;
  using System.Threading.Tasks;
  using Microsoft.ServiceBus.Messaging;
  
  public static void Run(BrokeredMessage message, TraceWriter log)
  {
      string body = message.GetBody&lt;string&gt;();
  
      using (var client = new HttpClient())
      {
          var content = new StringContent(body, Encoding.UTF8, "application/json");
          var response = client.PutAsync("http://some-rest-endpoint.url/api", content).Result;
          if (!response.IsSuccessStatusCode)
          {
              throw new Exception("Message could not be sent");
          }
      }    
  }
  </code></pre>
  
  <p>Does anyone know a better way to gracefully fail the function?</p>