Making blocking calls to async methods transforms code that was intended to be asynchronous into a blocking operation. Doing so inside
an Azure Function can lead to thread exhaustion.
| To Do This … | Instead of This … | Use This |
|---|---|---|
Retrieve the result of a background task |
|
|
Wait for any task to complete |
|
|
Retrieve the results of multiple tasks |
|
|
Wait a period of time |
|
|
public static class AvoidBlockingCalls
{
[FunctionName("Foo")]
public static async Task<IActionResult> Foo([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
{
// This can lead to thread exhaustion
string requestBody = new StreamReader(req.Body).ReadToEndAsync().Result;
// do stuff...
}
}
public static class AvoidBlockingCalls
{
[FunctionName("Foo")]
public static async Task<IActionResult> Foo([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
// do stuff...
}
}
async methods.