Running .NET Azure Functions on macOS and Visual Studio Code

TypeScript is typically my go-to language for building any solution, but sometimes, you must use what is best for the job. In my current project, I am using .NET Core to build Azure Functions, and I had to get myself familiar with using .Net Core and Azure Functions on macOS.

As the Microsoft documentation only explained it with Visual Studio for Mac, I had to figure out how to get it working with Visual Studio Code. This post will describe getting .NET Core and Azure Functions working on macOS with Visual Studio Code.

Prerequisites

To start building. NET-based Azure Functions, you need to install the .NET SDK and Azure Functions Core Tools on your machine.

Installing .NET SDK on macOS

The .NET-supported versions are found on the following download .NET page.

I went for the binaries of the .NET 8.0 SDK for macOS download .NET 8.0.201 SDK for macOS Arm64.

If you go for the binary download, once downloaded, you can move the files from the tar file to the directory of your choice. On the .NET download page, they recommend using the $HOME/dotnet directory. In my case, I moved the files to the $HOME/.dotnet directory.

Moving the .NET SDK files to the .dotnet directory
1
2
cd ~/Downloads
mkdir -p $HOME/.dotnet && tar zxf dotnet-sdk-8.0.201-osx-arm64.tar.gz -C $HOME/.dotnet
important

The filename can vary depending on which version you downloaded.

After moving the files, you must add the .NET SDK to your shell profile or any other profile you use.

In my case, I am using zsh, so I added the following to my .zshrc file and the .zprofile file.

  • Shell profile: ~/.profile
  • Bash profile: ~/.bash_profile, ~/.bashrc
  • Zsh profile: ~/.zshrc, ~/.zprofile

In those files, you need to add the following lines:

1
2
export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$HOME/.dotnet
info

By adding those lines, you permanently make dotnet commands available in your terminal.

After adding it, restart your terminal or run the following commands to apply the changes:

1
2
# Change the file to the profile you are using
source ~/.zshrc

Once your profile has been reloaded, you can check if the .NET SDK is installed by running the following command:

1
dotnet --info

Installing Azure Functions Core Tools

The documentation for installing the Azure Functions Core Tools on macOS is up to date. You can find it in the Install the Azure Functions Core Tools section of the Core Tools Development documentation.

1
2
3
4
5
6
# When Homebrew is installed
brew tap azure/functions
brew install azure-functions-core-tools@4

# To update the Azure Functions Core Tools
brew upgrade azure-functions-core-tools@4

To check if the Azure Functions Core Tools are installed, you can run the following command:

1
func --version

Creating a new Azure Functions project

You can create a new Azure Functions project after installing the .NET SDK and Azure Functions Core Tools. You have the option of creating a new project from your terminal or Visual Studio Code with the Azure Functions extension.

Using the terminal

To create a new Azure Functions project, you can use the following command:

1
func init

This command will ask you a few questions to set up the project. You can choose the following options:

  • Select the runtime: dotnet (isolated process)
  • Select the language: C#

If everything is installed correctly, it should create a new Azure Functions project in the current directory.

New Azure Functions can be added to the project by running the following command:

1
func new

Using Visual Studio Code

Suppose you have installed the Azure Functions extension in Visual Studio Code. In that case, you can create a new Azure Functions project by clicking on the Azure icon in the sidebar and then clicking the Create New Project button in the workspace section.

Show image Create a new Azure Function project
Create a new Azure Function project

It will ask you similar questions as the terminal command to set up the project.

Show image Select the Azure Functions runtime
Select the Azure Functions runtime

Running the Azure Functions

Now that you have created the Azure Functions project, you can run the Azure Functions locally. Start by opening the project in Visual Studio Code if you haven’t already. It should suggest installing the C# extension, which is required to debug the Azure Functions.

info

Additionally, you can install the C# Dev Kit.

You only need to press F5 to debug the Azure Functions. It will start the Azure Functions Core Tools and run the Azure Functions locally.

Show image Debugging the Azure Functions
Debugging the Azure Functions

Extra: Installing Azurite for local development

Azurite is a local emulator for Azure Storage. You can install Azurite with the following command:

1
npm install -g azurite

To run it locally, you can use the following command:

1
azurite
info

More information about Azurite can be found on the Use the Azurite emulator for local Azure Storage development documentation

Comments

Back to top