In the Azure functions "Performance considerations" part, Functions Best Practices, under "Use async code but avoid blocking calls", async
programming is the suggested practice for performance improvement. However, what is the best way to use it? For example, in my scenario, I have the following Service Bus Trigger:
public static void Run( [ServiceBusTrigger("topicname", "subname", AccessRights.Manage, Connection = "TopicConnection")]string message, TraceWriter log) { try { log.Info($"C# ServiceBus topic trigger function processed message: {message}"); Task.Run(() => PushToDb(message, log)); } catch(Exception ex) { log.Info($"Exception found {ex.Message}"); } }
In the above code, I call PushToDb
method async
. However, since it runs in the background, Function runtime assumes that the messages are consumed successfully and completes it. What if the PushToDb
method throws an exception? How can I make sure runtime knows that it's not complete, but rather should be abandoned?
Looking to use async
as much as possible for performance.
Azure Functions are best suited for smaller apps have events that can work independently of other websites. Some of the common azure functions are sending emails, starting backup, order processing, task scheduling such as database cleanup, sending notifications, messages, and IoT data processing.
Durable entities are currently not supported in Java. Entity functions are available in Durable Functions 2.0 and above for C#, JavaScript, and Python.
Organize your functions As part of your solution, you likely develop and publish multiple functions. These functions are often combined into a single function app, but they can also run in separate function apps.
You can make the function async:
public static async Task Run( [ServiceBusTrigger("topicname", "subname", AccessRights.Manage, Connection = "TopicConnection")]string message, TraceWriter log) { try { log.Info($"C# ServiceBus topic trigger function processed message: {message}"); await PushToDb(message, log); } catch(Exception ex) { log.Info($"Exception found {ex.Message}"); } }
The Functions runtime allows you to make your function async and return a Task.
In this case we can just await the call so we can handle exceptions normally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With