Skip to content

Files

Latest commit

ac31058 · Dec 29, 2021

History

History
158 lines (90 loc) · 10.8 KB

functions-create-your-first-function-visual-studio-uiex.md

File metadata and controls

158 lines (90 loc) · 10.8 KB
title description ms.assetid ms.topic ms.date ms.devlang ms.custom ROBOTS
Quickstart: Create your first function in Azure using Visual Studio
In this quickstart, you learn how to create and publish an HTTP trigger Azure Function by using Visual Studio.
82db1177-2295-4e39-bd42-763f6082e796
quickstart
09/30/2020
csharp
devx-track-csharp, mvc, devcenter, vs-azure, 23113853-34f2-4f, mode-ui
NOINDEX,NOFOLLOW

Quickstart: Create your first function in Azure using Visual Studio

In this article, you use Visual Studio to create a C# class library-based function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.

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

1. Prepare your environment


Use an Azure Functions project instead If you want to create an Azure Functions project by using Visual Studio 2017 instead, you must first install the [latest Azure Functions tools](functions-develop-vs.md#check-your-tools-version).

2. Create a function app project

  1. From the Visual Studio menu, select File > New > Project.

  2. In Create a new project, enter functions in the search box, choose the Azure Functions template, and then select Next.

  3. In Configure your new project, enter a Project name for your project, and then select Create.

  4. Provide the following information for the Create a new Azure Functions application settings:

    • Select Azure Functions v3 (.NET Core) from the Functions runtime dropdown. (For more information, see Azure Functions runtime versions overview.)

    • Select HTTP trigger as the function template.

    • Select Storage emulator from the Storage account dropdown.

    • Select Anonymous from the Authorization level dropdown. (For more information about keys and authorization, see Authorization keys and HTTP and webhook bindings.)

    • Select Create

3. Rename the function

The FunctionName method attribute sets the name of the function, which by default is generated as Function1. Because the tooling doesn't let you override the default function name when you create your project, take a minute to create a better name for the function class, file, and metadata.

  1. In File Explorer, right-click the Function1.cs file and rename it to HttpExample.cs.

  2. In the code, rename the Function1 class to `HttpExample'.

  3. In the HttpTrigger method named Run, rename the FunctionName method attribute to HttpExample.

4. Run the function locally

  1. To run your function, press F5 in Visual Studio.

  2. Copy the URL of your function from the Azure Functions runtime output.

    Azure local runtime

  3. Paste the URL for the HTTP request into your browser's address bar. Append the query string ?name=<YOUR_NAME> to this URL and run the request.

    Function localhost response in the browser

  4. To stop debugging, press Shift+F5 in Visual Studio.


Troubleshooting You might need to enable a firewall exception so that the tools can handle HTTP requests. Authorization levels are never enforced when you run a function locally.

5. Publish the project to Azure

  1. In Solution Explorer, right-click the project and select Publish.

  2. In Target, select Azure

    :::image type="content" source="../../includes/media/functions-vstools-publish/functions-visual-studio-publish-profile-step-1.png" alt-text="Select Azure target":::

  3. In Specific target, select Azure Function App (Windows)

    :::image type="content" source="../../includes/media/functions-vstools-publish/functions-visual-studio-publish-profile-step-2.png" alt-text="Select Azure Function App":::

  4. In Function Instance, select Create a new Azure Function... and then use the values specified in the following:

    • For Name provide a Globally unique name

    • Select a subscription from the drop-down list.

    • Select an existing resource group from the drop-down list or choose New to create a new resource group.

    • Select Consumption in the Play Type drop-down. (For more information, see Consumption plan.)

    • Select an location from the drop-down.

    • Select an <abbr="An Azure Storage account is required by the Functions runtime. Select New to configure a general-purpose storage account. You can also choose an existing account that meets the storage account requirements.">Azure Storage account from the drop-down

    Create App Service dialog

  5. Select Create

  6. In the Functions instance, make sure that Run from package file is checked.

    :::image type="content" source="../../includes/media/functions-vstools-publish/functions-visual-studio-publish-profile-step-4.png" alt-text="Finish profile creation":::


    What does this setting do? When using **Run from package file**, your function app is deployed using [Zip Deploy](functions-deployment-technologies.md#zip-deploy) with [Run-From-Package](run-functions-from-deployment-package.md) mode enabled. This is the recommended deployment method for your functions project, since it results in better performance.
  7. Select Finish.

  8. On the Publish page, select Publish.

  9. On the Publish page, review the root URL of the function app.

  10. In the Publish tab, choose Manage in Cloud Explorer.

    :::image type="content" source="../../includes/media/functions-vstools-publish/functions-visual-studio-publish-complete.png" alt-text="Publish success message":::

6. Test your function in Azure

  1. In Cloud Explorer, your new function app should be selected. If not, expand your subscription, expand App Services, and select your new function app.

  2. Right-click the function app and choose Open in Browser. This opens the root of your function app in your default web browser and displays the page that indicates your function app is running.

    :::image type="content" source="media/functions-create-your-first-function-visual-studio/function-app-running-azure.png" alt-text="Function app running":::

  3. In the address bar in the browser, append the string /api/HttpExample?name=Functions to the base URL and run the request.

    The URL that calls your HTTP trigger function is in the following format:

    http://<APP_NAME>.azurewebsites.net/api/HttpExample?name=Functions

  4. Go to this URL and you see a response in the browser to the remote GET request returned by the function, which looks like the following example:

    :::image type="content" source="media/functions-create-your-first-function-visual-studio/functions-create-your-first-function-visual-studio-browser-azure.png" alt-text="Function response in the browser":::

7. Clean up resources

[!INCLUDE functions-vstools-cleanup]

Next steps

Advance to the next article to learn how to add an Azure Storage queue output binding to your function:

[!div class="nextstepaction"] Add an Azure Storage queue binding to your function