Skip to content

Latest commit

 

History

History
115 lines (76 loc) · 7.34 KB

create-first-function-vs-code-csharp.md

File metadata and controls

115 lines (76 loc) · 7.34 KB
title description ms.topic ms.date ms.devlang 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
06/11/2022
csharp
devx-track-csharp, mode-ui, vscode-azure-extension-update-complete
true
DocsExp–386541–A/B–Enhanced-Readability-Quickstarts–2.19.2021
Experience B
./create-first-function-vs-code-csharp-ieux

Quickstart: Create a C# function in Azure using Visual Studio Code

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. This article creates an HTTP triggered function that runs on .NET 6.0. There's also a CLI-based version of this article.

By default, this article shows you how to create C# functions that runs on .NET 6 in the same process as the Functions host. These in-process C# functions are only supported on Long Term Support (LTS) versions of .NET, such as .NET 6. To create C# functions on .NET 6 that can also run on .NET 5.0 and .NET Framework 4.8 (in preview) in an isolated process, see the alternate version of this article.

Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.

Configure your environment

Before you get started, make sure you have the following requirements in place:

You also need an Azure account with an active subscription. Create an account for free.

Create your local project

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.

  1. Choose the Azure icon in the Activity bar, then in the Workspace (local) area, select the + button, choose Create Function in the dropdown. When prompted, choose Create new project.

    :::image type="content" source="./media/functions-create-first-function-vs-code/create-new-project.png" alt-text="Screenshot of create a new project window.":::

  2. Select the directory location for your project workspace and choose Select. You should either create a new folder or choose an empty folder for the project workspace. Don't choose a project folder that is already part of a workspace.

  3. Provide the following information at the prompts:

    Prompt Selection
    Select a language Choose C#.
    Select a .NET runtime Select .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 Select Add to workspace.
    Prompt Selection
    Select a language 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 for Azure Functions: Project Runtime and change the default runtime version to ~4.
  4. Visual Studio Code uses the provided information and generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. For more information about the files that are created, see Generated project files.

[!INCLUDE functions-run-function-test-local-vs-code-csharp]

After checking 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]

Next steps

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