Reader small image

You're reading from  Software Architecture with C# 12 and .NET 8 - Fourth Edition

Product typeBook
Published inFeb 2024
PublisherPackt
ISBN-139781805127659
Edition4th Edition
Right arrow
Authors (2):
Gabriel Baptista
Gabriel Baptista
author image
Gabriel Baptista

Gabriel Baptista has been working with software development since the beginning of .NET. Today, his main contributions are managing numerous projects for retail and industry. He is an Azure Platform-as-a-Service (PaaS) solution specialist, teaches at Computing Engineering universities, and helps tech startups as a mentor.
Read more about Gabriel Baptista

Francesco Abbruzzese
Francesco Abbruzzese
author image
Francesco Abbruzzese

Francesco Abbruzzese dedicates his life to his two great passions: software and powerlifting. He is the author of the MVC Controls Toolkit and the Blazor Controls Toolkit libraries. He has contributed to the diffusion and evangelization of the Microsoft web stack since the first version of ASP.NET. His company, Mvcct Team, offers web applications, tools, and services for web technologies. He has moved from AI systems, where he implemented one of the first decision support systems for financial institutions, to top-10 video game titles such as Puma Street Soccer.
Read more about Francesco Abbruzzese

View More author details
Right arrow

Working with Serverless – Azure Functions

In Chapter 10, Deciding on the Best Cloud-Based Solution, we explored the fundamentals and strategic advantages of different cloud architectures and serverless can be considered one of the newest ways to provide flexible cloud-based solutions. We delved into how serverless systems offer scalability, cost-efficiency, and agility – key factors that drive today’s software architecture decisions.

Building on that foundation, this chapter delves deeper into a pivotal component of serverless architecture: Azure Functions.

Azure Functions stands out as the component that Microsoft delivers as a prime example of serverless architecture in action. It offers a versatile, event-driven approach that integrates seamlessly with the .NET ecosystem, making it the for architects and developers aiming to build efficient, scalable, and responsive applications.

We will navigate through the intricacies of Azure Functions, emphasizing...

Technical requirements

This chapter requires that you have the following:

  • The free Visual Studio 2022 Community Edition or, even better, with all the Azure tools installed.
  • A free Azure account. The Creating an Azure account section of Chapter 1, Understanding the Importance of Software Architecture, explains how to create one.

Understanding the Azure Functions app

The Azure Functions app is an Azure PaaS (platform as a service) where you can build pieces of code (functions), connect them to your application, and use triggers to start them. The concept is quite simple – you build a function in the language you prefer and decide on the trigger that will start it. You can write as many functions as you want in your system. There are cases where the system is written entirely with functions.

The steps to create the necessary environment are as simple as the ones we need to follow to create the function itself. The following screenshot shows the parameters that you must decide on when you create the environment. After you select Create a resource in Azure and filter by Function App, upon clicking the Create button, you will see the following screen:

Figure 16.1: Creating an Azure function app

There are a couple of key points that you should consider while creating your Azure Functions...

Programming Azure Functions using C#

In this section, you will learn how to create Azure Functions. It is worth mentioning that there are several ways to create them using C#. The first one is by creating the functions and developing them in the Azure portal itself. To do this, let us assume that you have created an Azure Functions app with similar configurations to the ones in the screenshot at the beginning of the chapter.

By selecting the resource created and navigating to the Functions menu, you will be able to add new functions to this environment, as you can see in the following screenshot:

Figure 16.2: Adding a function

Here, you will need to decide the kind of trigger that you want to use to start the execution. The most frequently used ones are the HTTP trigger and the Timer trigger. The first enables the creation of an HTTP API that will trigger the function. The second means functions will be triggered by a timer you set.

When you decide on the trigger...

Maintaining Azure Functions

Once you have created and programmed your function, you need to monitor and maintain it. To do this, you can use a variety of tools, all of which you can find in the Azure portal. These tools will help you solve problems due to the amount of information you will be able to collect with them.

The first option when it comes to monitoring your function is using the Monitor menu inside the Azure Functions interface in the Azure portal. There, you will be able to check all your function executions, including successful results and failures:

Figure 16.7: Monitoring a function

It will take about 5 minutes for any results to be available. The date shown in the grid is in UTC time.

By clicking on Run query in Application Insights, the same interface allows you to connect to this tool. This will take you to a world of almost infinite options that you can use to analyze your function data. Application Insights is an excellent option for Application...

Azure Durable Functions

If you decide to delve deeper into the usage of serverless, you may consider Azure Durable Functions as a good option for designing orchestration scenarios. Azure Durable Functions let us write stateful workflows, managing the state behind the scenes. To do so, you will have to write an orchestrator function, which is basically a procedure that defines the workflow that you want to run. You may also need some entity functions to enable the reading of small pieces of state.

The following are some application patterns where this solution can be used; however, it is important to remember that it is not suitable for all applications:

  • Function chaining: When you need to execute a sequence of functions in a particular order.
  • Async HTTP APIs: A good way to solve long-running operations with external clients, where you will have the opportunity to get a status API because of the orchestrator function. There is a sample code of this pattern as soon...

Azure Functions roadmap

The structure of Azure Functions has changed since it was rolled out in 2016. The number of people using the tool and the changes related to .NET caused some compatibility problems that saw Microsoft come up with a new way of delivering the deployment of functions. This new way is called the isolated process model, and it has been available since .NET 5. It is also important to mention that the currently supported available runtime versions for Azure Functions are v1 and v4.

According to the current roadmap, using the isolated process model is the only way to run Azure Functions in .NET 8 and future versions. There is a plan to have the in-process model for .NET 8, but no date has been confirmed yet.

thumbnail image 1 captioned A diagram showing the change in release patterns after parity. .NET 8 has an in-process model option on a delay after the isolated worker model. All subsequent updates use the isolated worker model.

Figure 16.10: Azure Functions roadmap

As a software architect, you must keep an eye on the roadmaps provided so that you can decide on the best implementation for your solution.

When implementing an Azure function using the isolated process...

The decision to use serverless and Azure Functions

Even with the benefits presented during this chapter, there is always a question about why one would use a serverless function over having it as part of a bigger web application.

If you think exclusively about HTTP Trigger functions, this question is more difficult to answer because you can create a Web API application that generally solves the problems in this scenario.

However, there are some use cases where an Azure Function is truly the best option. Let’s list them to help you with this decision in your scenario:

Summary

In this chapter, we looked at some of the advantages of developing functionality with serverless Azure Functions. You can use it as a guideline to check the different types of triggers that are available in Azure Functions and to plan how to monitor them. We also saw how to program and maintain Azure Functions.

In the next chapter, we will discuss the current news related to ASP.NET Core MVC.

Questions

  1. What are Azure Functions?
  2. What are the programming options for Azure Functions?
  3. What are the plans that can be used with Azure Functions?
  4. How can you deploy Azure Functions with Visual Studio?
  5. What triggers can you use to develop Azure Functions?
  6. What is the difference between Azure Functions v1, v2, v3, and v4?
  7. How does Application Insights help us to maintain and monitor Azure Functions?
  8. What are Azure Durable Functions?

Further reading

If you want to learn more about creating Azure functions, check out the following links:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Software Architecture with C# 12 and .NET 8 - Fourth Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781805127659
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Gabriel Baptista

Gabriel Baptista has been working with software development since the beginning of .NET. Today, his main contributions are managing numerous projects for retail and industry. He is an Azure Platform-as-a-Service (PaaS) solution specialist, teaches at Computing Engineering universities, and helps tech startups as a mentor.
Read more about Gabriel Baptista

author image
Francesco Abbruzzese

Francesco Abbruzzese dedicates his life to his two great passions: software and powerlifting. He is the author of the MVC Controls Toolkit and the Blazor Controls Toolkit libraries. He has contributed to the diffusion and evangelization of the Microsoft web stack since the first version of ASP.NET. His company, Mvcct Team, offers web applications, tools, and services for web technologies. He has moved from AI systems, where he implemented one of the first decision support systems for financial institutions, to top-10 video game titles such as Puma Street Soccer.
Read more about Francesco Abbruzzese