Skip to content

Latest commit

 

History

History
93 lines (59 loc) · 6.38 KB

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

File metadata and controls

93 lines (59 loc) · 6.38 KB
title description ms.topic ms.date ms.devlang ms.custom
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
01/05/2023
csharp
devx-track-csharp, mode-ui, vscode-azure-extension-update-complete

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

This article creates an HTTP triggered function that runs on .NET. For information about .NET versions supported for C# functions, see Supported versions.

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.

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. For Select a language, choose C#.

  4. For Select a .NET runtime, choose from one of the following options:

    Option .NET version Process model Description
    .NET 6.0 (LTS) .NET 6 In-process In-process C# functions are only supported on Long Term Support (LTS) .NET versions. Function code runs in the same process as the Functions host.
    .NET 6.0 Isolated (LTS) .NET 6 Isolated worker process Functions run on .NET 6, but in a separate process from the Functions host.
    .NET 7.0 Isolated .NET 7 Isolated worker process Because .NET 7 isn't an LTS version of .NET, your functions must run in an isolated process on .NET 7.
    .NET Framework Isolated .NET 7 Isolated worker process Choose this option when your functions need to use libraries only supported on the .NET Framework.

    The two process models use different APIs, and each process model uses a different template when generating the function project code. If you don't see these options, press F1 and type Preferences: Open user settings, then search for Azure Functions: Project Runtime and make sure that the default runtime version is set to ~4.

  5. Provide the remaining information at the prompts:

    Prompt Selection
    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 Open in current window.
  6. 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.

The next article depends on your chosen process model.

[!div class="nextstepaction"] Connect to Azure Cosmos DB Connect to Azure Queue Storage Connect to Azure SQL

[!div class="nextstepaction"] Connect to Azure Cosmos DB Connect to Azure Queue Storage