Skip to content

Latest commit

 

History

History
107 lines (72 loc) · 7.11 KB

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

File metadata and controls

107 lines (72 loc) · 7.11 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
11/08/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

This article creates an HTTP triggered function that runs on .NET 6, either in-process or isolated worker process. .NET Functions isolated worker process also lets you run on .NET 7 (in preview). For information about all .NET versions supported by isolated worker process, see Supported versions.

There's also a CLI-based version of this article.

By default, this article shows you how to create C# functions that run on .NET 6 in the same process as the Functions host. These in-process C# functions are only supported on Long Term Support (LTS) .NET versions, such as .NET 6. When creating your project, you can choose to instead create a function that runs on .NET 6 in an isolated worker process. Isolated worker process supports both LTS and Standard Term Support (STS) versions of .NET. For more information, see Supported versions in the .NET Functions isolated worker process guide.

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:

[!INCLUDE functions-requirements-visual-studio-code-csharp]

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 or other available .NET SDK versions, from .NET website here.
    • 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