Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Practical Serverless and Microservices with C#

You're reading from   Practical Serverless and Microservices with C# Build resilient and secure microservices with the .NET stack and embrace serverless development in Azure

Arrow left icon
Product type Paperback
Published in Jul 2025
Publisher Packt
ISBN-13 9781836642015
Length 472 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Gabriel Baptista Gabriel Baptista
Author Profile Icon Gabriel Baptista
Gabriel Baptista
Francesco Abbruzzese Francesco Abbruzzese
Author Profile Icon Francesco Abbruzzese
Francesco Abbruzzese
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Demystifying Serverless Applications 2. Demystifying Microservices Applications FREE CHAPTER 3. Setup and Theory: Docker and Onion Architecture 4. Azure Functions and Triggers Available 5. Background Functions in Practice 6. IoT Functions in Practice 7. Microservices in Practice 8. Practical Microservices Organization with Kubernetes 9. Simplifying Containers and Kubernetes: Azure Container Apps, and Othert Tools 10. Security and Observability for Serverless and Microservices Applications 11. The Car Sharing App 12. Simplifying Microservices with .NET Aspire 13. Other Books You May Enjoy
14. Index

Coding Azure functions using VS Code

Creating an HTTP trigger Azure function using VS Code involves several well-defined steps. Here is a detailed guide to help you through the process.

There are some prerequisites to enable the development of Azure functions using VS Code, as follows:

  • Ensure you have VS Code installed on your machine. The use of VS Code will help you not only develop the Azure functions needed but also manage your Azure account using the Azure Tools extension.
  • It is recommended that you sign in to your Azure account to create the new function. The C# Dev Kit may also be installed.
  • The GitHub Copilot extension can also be installed to help you solve coding problems and, at the same time, guide you while writing code.
  • Install the Azure Functions extension for VS Code. This VS Code extension will facilitate the development of functions, giving you wizards for each function trigger desired.
  • Install the Azurite extension for VS Code. This VS Code extension is an open source Azure Storage API-compatible server for debugging Azure Functions locally.
  • Make sure you have the Azure Functions Core Tools, and the .NET SDK installed if you are using C#.

Once you have set up your environment, you will have something like the following figure:

Figure 1.6: VS Code ready to write Azure functions

Figure 1.6: VS Code ready to write Azure functions

  • Once all the prerequisites are set, in the Azure tab, go to WORKSPACE and select Create Function Project…. Next, perform the following steps:
    1. Choose a location for your project and select your preferred programming language.
    2. Follow the prompts to create a new HTTP trigger function. You can name it Health and call the namespace CarShare.Function.
    3. You will need to decide on the access rights for this function. For this example, you can choose Anonymous. We will discuss each of the security options later.
    4. Open the newly created function file. You will see a template code for an HTTP trigger function.
    5. Modify the function to meet your specific requirements, which, in this case, means to respond if the function is working properly. Notice that this is a GET and POST function. For the purpose we have defined, you can change the code to only be an HTTP GET function.
    6. Save your changes.

For running and debugging locally, you just need to press F5 or navigate to Run > Start Debugging. VS Code will start the Azure Functions host, and you will see the function URL in the output window. Then, you can use tools such as Postman or your browser to send HTTP requests to your function endpoint.

It is worth mentioning that for running Azure Functions locally, you will need to allow PowerShell scripts to run without being digitally signed. This can be a problem depending on the security policies provided by your company.

Once the function is running, you can consider it the same as when you work on other types of software projects, and even the debugging will work properly. The trigger will depend on the function you set. The following figure shows the code of the function program, where you can see the response to the caller with a status of 200 by using OkObjectResult with the message “Yes! The function is live!” and the UTC time.

Figure 1.7: Azure Functions running locally

Figure 1.7: Azure Functions running locally

As you have created a function app connected to a GitHub repository with the deployment process handled by GitHub Actions, once you commit and pull the code to GitHub, GitHub Actions will automatically build the function and deploy it as a function app.

Figure 1.8: Function app deployed using GitHub Actions

Figure 1.8: Function app deployed using GitHub Actions

It is not the purpose of this book to discuss CI/CD strategies, but you will certainly need to think about them when it comes to professional development.

The result of this deployment can be checked in the Azure portal, where the function developed will be available in the list of functions. It is worth noting that a function app can handle more than one function at the same time.

Figure 1.9: Health function available in the function app

Figure 1.9: Health function available in the function app

The function can be executed as soon as it is published to Azure. As a result of the sample function, as this was developed as a GET HTTP trigger, we can check that the function is working by accessing the API in the web browser.

Figure 1.10: Health function running properly

Figure 1.10: Health function running properly

As you don’t have a live CI/CD pipeline, you can also publish your Azure function directly from the VS Code IDE. To do so, you may use the Azure Functions extension provided by VS Code.

There are a few steps to follow in this case. The first one is to select the action to deploy the function in the VS Code prompt:

Figure 1.11: Deploying to Azure using VS Code

Figure 1.11: Deploying to Azure using VS Code

After that, you will need to select the corresponding subscription and the name of the new function app you want to deploy, considering a new function:

Figure 1.12: Creating a new function app

Figure 1.12: Creating a new function app

The current process proposed by the extension is to deploy an Azure function in the Flex Consumption plan. There are some specific locations where this option is available:

Figure 1.13: Defining the location for the new function app

Figure 1.13: Defining the location for the new function app

The definition of the runtime stack is also important to get the most out of your Azure function. In the case of the Flex Consumption plan, you will also be asked for the memory usage in the instance and the maximum number of instances available for parallel calls.

Figure 1.14: Defining the runtime stack for the new function app

Figure 1.14: Defining the runtime stack for the new function app

Once these sets are defined, your Azure function will be deployed correctly. You can also redeploy functions using the same technique later, without needing to recreate the Azure function app every single time.

Figure 1.15: Function app properly deployed

Figure 1.15: Function app properly deployed

Last, but not least, the Azure portal also gives you the possibility to monitor and manage the functions deployed. Once this process is done, you can monitor your function’s performance and log. By using the Monitoring section of your function app, you can view execution details, track failures, and analyze performance metrics.

lock icon The rest of the chapter is locked
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.
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 €18.99/month. Cancel anytime