Skip to content

Files

Latest commit

7e303ca · Mar 18, 2022

History

History
320 lines (192 loc) · 14.1 KB

create-first-function-cli-csharp-ieux.md

File metadata and controls

320 lines (192 loc) · 14.1 KB
title description ms.date ms.topic ms.devlang ms.custom ROBOTS
Create a C# function from the command line - Azure Functions
Learn how to create a C# function from the command line, then publish the local project to serverless hosting in Azure Functions.
10/03/2020
quickstart
csharp
devx-track-csharp, devx-track-azurecli, devx-track-azurepowershell, mode-other
NOINDEX,NOFOLLOW

Quickstart: Create a C# function in Azure from the command line

[!div class="op_single_selector" title1="Select your function language: "]

In this article, you use command-line tools 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.

There is also a Visual Studio Code-based version of this article.

1. Prepare your environment


2. Verify prerequisites

Verify your prerequisites, which depend on whether you are using the Azure CLI or Azure PowerShell for creating Azure resources:

  • In a terminal or command window, run func --version to check that the Azure Functions Core Tools are version 3.x.

  • Run az --version to check that the Azure CLI version is 2.4 or later.

  • Run az login to sign in to Azure and verify an active subscription.

  • Run dotnet --list-sdks to check that .NET Core SDK version 3.1.x is installed

+Run func --version to check that the Azure Functions Core Tools are version 3.x.

  • Run (Get-Module -ListAvailable Az).Version and verify version 5.0 or later.

  • Run Connect-AzAccount to sign in to Azure and verify an active subscription.

  • Run dotnet --list-sdks to check that .NET Core SDK version 3.1.x is installed


3. Create a local function project

In this section, you create a local Azure Functions project in C#. Each function in the project responds to a specific trigger.

  1. Run the func init command to create a functions project in a folder named LocalFunctionProj with the specified runtime:

    func init LocalFunctionProj --dotnet
  2. Run 'cd LocalFunctionProj' to navigate to the project folder.

    cd LocalFunctionProj

  3. Add a function to your project by using the following command:

    func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"

    The --name argument is the unique name of your function (HttpExample).

    The --template argument specifies the function's trigger (HTTP).


    Optional: Code for HttpExample.cs

    HttpExample.cs contains a Run method that receives request data in the req variable is an HttpRequest that's decorated with the HttpTriggerAttribute, which defines the trigger behavior.

    :::code language="csharp" source="~/functions-docs-csharp/http-trigger-template/HttpExample.cs":::

    The return object is an ActionResult that returns a response message as either an OkObjectResult (200) or a BadRequestObjectResult (400). To learn more, see Azure Functions HTTP triggers and bindings.



4. Run the function locally

  1. Run your function by starting the local Azure Functions runtime host from the LocalFunctionProj folder:

    func start
    

    Toward the end of the output, the following lines should appear:

     ...
    
     Now listening on: http://0.0.0.0:7071
     Application started. Press Ctrl+C to shut down.
    
     Http Functions:
    
             HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
     ...
    
     

    I don't see HttpExample in the output

    If HttpExample doesn't appear, you likely started the host from outside the root folder of the project. In that case, use Ctrl+C to stop the host, navigate to the project's root folder, and run the previous command again.

  2. Copy the URL of your HttpExample function from this output to a browser and append the query string ?name=<YOUR_NAME>, making the full URL like http://localhost:7071/api/HttpExample?name=Functions. The browser should display a message like Hello Functions:

    Result of the function run locally in the browser

  3. Select Ctrl+C and choose y to stop the functions host.



5. Create supporting Azure resources for your function

Before you can deploy your function code to Azure, you need to create a resource group, a storage account, and a function app by using the following commands:

  1. If you haven't done so already, sign in to Azure:

    az login
    
    Connect-AzAccount
    

  2. Create a resource group named AzureFunctionsQuickstart-rg in the westeurope region.

    az group create --name AzureFunctionsQuickstart-rg --location westeurope
    

    The az group create command creates a resource group. You generally create your resource group and resources in a region near you, using an available region returned from the az account list-locations command.

    New-AzResourceGroup -Name AzureFunctionsQuickstart-rg -Location westeurope
    

    You can't host Linux and Windows apps in the same resource group. If you have an existing resource group named AzureFunctionsQuickstart-rg with a Windows function app or web app, you must use a different resource group.

  3. Create a general-purpose Azure Storage account in your resource group and region:

    az storage account create --name <STORAGE_NAME> --location westeurope --resource-group AzureFunctionsQuickstart-rg --sku Standard_LRS
    
    New-AzStorageAccount -ResourceGroupName AzureFunctionsQuickstart-rg -Name <STORAGE_NAME> -SkuName Standard_LRS -Location westeurope
    

    Replace <STORAGE_NAME> with a name that is appropriate to you and unique in Azure Storage. Names must contain three to 24 characters numbers and lowercase letters only. Standard_LRS specifies a general-purpose account, which is supported by Functions.

  4. Create the function app in Azure. Replace '<STORAGE_NAME>** with name in previous step. Replace '<APP_NAME>' with a globally unique name.

    az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location westeurope --runtime dotnet --functions-version 3 --name <APP_NAME> --storage-account <STORAGE_NAME>
    
    New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccount <STORAGE_NAME> -Runtime dotnet -FunctionsVersion 3 -Location 'West Europe'
    

    Replace <STORAGE_NAME> with the name of the account you used in the previous step.

    Replace <APP_NAME> with a unique name. The <APP_NAME> is also the default DNS domain for the function app.


    What is the cost of the resources provisioned on Azure?

    This command creates a function app running in your specified language runtime under the Azure Functions Consumption plan, which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see Monitor Azure Functions. The instance incurs no costs until you activate it.



6. Deploy the function project to Azure

Copy ' func azure funtionapp publish <APP_NAME> into your terminal Replace <APP_NAME> with the name of your app. Run

func azure functionapp publish <APP_NAME>

The publish command shows results similar to the following output (truncated for simplicity):

...

Getting site publishing info...
Creating archive for current directory...
Performing remote build for functions project.

...

Deployment successful.
Remote build succeeded!
Syncing triggers...
Functions in msdocs-azurefunctions-qs:
    HttpExample - [httpTrigger]
        Invoke url: https://msdocs-azurefunctions-qs.azurewebsites.net/api/httpexample


7. Invoke the function on Azure

Copy the complete Invoke URL shown in the output of the publish command into a browser address bar. Append the query parameter &name=Functions.

The output of the function run on Azure in a browser



8. Clean up resources

If you continue to the next step and add an Azure Storage queue output binding, keep all your resources in place as you'll build on what you've already done.

Otherwise, use the following command to delete the resource group and all its contained resources to avoid incurring further costs.

az group delete --name AzureFunctionsQuickstart-rg
Remove-AzResourceGroup -Name AzureFunctionsQuickstart-rg



Next steps

[!div class="nextstepaction"] Connect to an Azure Storage queue