title | description | ms.topic | ms.date | ms.custom | adobe-target | adobe-target-activity | adobe-target-experience | adobe-target-content |
---|---|---|---|---|---|---|---|---|
Create a C# function using Visual Studio Code - Azure Functions |
Learn how to create a C# function, then publish the local project to serverless hosting in Azure Functions using the Azure Functions extension in Visual Studio Code. |
quickstart |
09/14/2021 |
devx-track-csharp |
true |
DocsExp–386541–A/B–Enhanced-Readability-Quickstarts–2.19.2021 |
Experience B |
./create-first-function-vs-code-csharp-ieux |
In this article, you use Visual Studio Code to create a C# function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
[!INCLUDE functions-dotnet-execution-model]
This article creates an HTTP triggered function that runs on .NET 6.0. There's also a CLI-based version of this article.
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
Before you get started, make sure you have the following requirements in place:
-
Azure Functions Core Tools version 4.x.
-
Visual Studio Code on one of the supported platforms.
-
C# extension for Visual Studio Code.
-
Azure Functions extension for Visual Studio Code.
You also need an Azure account with an active subscription. Create an account for free.
In this section, you use Visual Studio Code to create a local Azure Functions project in C#. Later in this article, you'll publish your function code to Azure.
-
Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project... icon.
-
Choose a directory location for your project workspace and choose Select.
[!NOTE] These steps were designed to be completed outside of a workspace. In this case, do not select a project folder that is part of a workspace.
-
Provide the following information at the prompts:
Prompt Selection Select a language for your function project Choose C#
.Select a .NET runtime Choose .NET 6
.Select a template for your project's first function Choose HTTP trigger
.Provide a function name Type HttpExample
.Provide a namespace Type My.Functions
.Authorization level Choose Anonymous
, which enables anyone to call your function endpoint. To learn about authorization level, see Authorization keys.Select how you would like to open your project Choose Add to workspace
.Prompt Selection Select a language for your function project Choose C#
.Select a .NET runtime Choose .NET 6 Isolated
.Select a template for your project's first function Choose HTTP trigger
.Provide a function name Type HttpExample
.Provide a namespace Type My.Functions
.Authorization level Choose Anonymous
, which enables anyone to call your function endpoint. To learn about authorization level, see Authorization keys.Select how you would like to open your project Choose Add to workspace
.
[!NOTE] If you don't see .NET 6 as a runtime option, check the following:
- Make sure you have installed the .NET 6.0 SDK.
- Press F1 and type
Preferences: Open user settings
, then search forAzure Functions: Project Runtime
and change the default runtime version to~4
.
-
Using this information, Visual Studio Code generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. To learn more about files that are created, see Generated project files.
[!INCLUDE functions-run-function-test-local-vs-code-csharp]
After you've verified that the function runs correctly on your local computer, it's time to use Visual Studio Code to publish the project directly to Azure.
[!INCLUDE functions-sign-in-vs-code]
[!INCLUDE functions-publish-project-vscode]
[!INCLUDE functions-vs-code-run-remote]
[!INCLUDE functions-cleanup-resources-vs-code.md]
You have used Visual Studio Code to create a function app with a simple HTTP-triggered function. In the next article, you expand that function by connecting to either Azure Cosmos DB or Azure Queue Storage. To learn more about connecting to other Azure services, see Add bindings to an existing function in Azure Functions.
[!div class="nextstepaction"] Connect to Azure Cosmos DB Connect to Azure Queue Storage
[!div class="nextstepaction"] Connect to Azure Cosmos DB Connect to Azure Queue Storage