DEV Community

Cover image for Developing Asynchronous Serverless Functions with ASP.NET Core Azure Functions
Janki Mehta
Janki Mehta

Posted on

Developing Asynchronous Serverless Functions with ASP.NET Core Azure Functions

**Azure **Functions allow you to run event-driven serverless code without managing infrastructure. Functions can be triggered by HTTP requests, schedules, database changes, file uploads, and more.

ASP.NET Core provides a streamlined way to build Azure Functions using C# and .NET. In this post, we'll look at how to develop asynchronous Azure Functions with ASP.NET Core.

Benefits of Serverless Functions
Some key benefits of using serverless functions:

  • No infrastructure to manage - Azure handles resource provisioning and scaling
  • Pay per execution pricing - Consume only the resources you need
  • Event-driven scale - Functions scale seamlessly based on demand
  • Faster development - No need to manage threading, connections, etc.

Creating an ASP.NET Core Azure Function

Let's walk through creating a basic HTTP triggered function using the Azure Functions template in Visual Studio:

  • Create a new Azure Functions project
  • Select HTTP trigger template
  • Modify Run method:
public static async Task<IActionResult> Run(HttpRequest req)
{
  return new OkObjectResult("Hello World!"); 
}
Enter fullscreen mode Exit fullscreen mode

This will return "Hello World!" when the function URL is accessed.

Making Asynchronous Functions

To make our function asynchronous, we can use the async and await keywords:

public static async Task<IActionResult> Run(HttpRequest req)  
{
  await Task.Delay(1000);
  return new OkObjectResult("Hello World!");
}
Enter fullscreen mode Exit fullscreen mode

This delays the response by 1 second to simulate latency.

The Run method signature returns a Task, allowing it to run asynchronously.

Accessing the Request and Response

We can access details about the HTTP request via the HttpRequest parameter:

var name = req.Query["name"];

return new OkObjectResult($"Hello {name}!");
Enter fullscreen mode Exit fullscreen mode

This reads the name query parameter and personalizes the response.

We can also construct the HTTP response by returning an IActionResult or setting HttpResponse.

Function Triggers and Bindings

Functions can be triggered by timers, queues, other Azure services, and more. We can access the trigger payload via parameter binding:

public static async Task Run(
  [QueueTrigger("messagequeue")] string message, 
  ILogger log)
{
  log.LogInformation(message);
}
Enter fullscreen mode Exit fullscreen mode

This queue-triggered function processes queue messages.

Input and output bindings streamline accessing services like Blob Storage from the function.

Dependency Injection

ASP.NET Core Dependency Injection allows injecting services into functions:

public class MyFunction 
{
  private readonly ILogger<MyFunction> logger;

  public MyFunction(ILogger<MyFunction> logger)
  {
    this.logger = logger;
  }

  public async Task Run(...) {
    logger.LogInformation("Function processed request");
  }
}
Enter fullscreen mode Exit fullscreen mode

The function class can now access the logger service via DI.

Some key points:

  • ASP.NET Core is a great fit for writing Azure Functions with .NET
  • Support for async/await provides efficient asynchronous code
  • Triggers and bindings simplify connecting to other services
  • Built-in features like DI enable loosely coupled functions With Azure Functions and ASP.NET Core, you can quickly build scalable event-driven apps without managing servers. Learn about the Common Architectures in ASP.NET Core in detail.

Top comments (0)