title | description | ms.date | ms.topic | ms.custom | adobe-target | adobe-target-activity | adobe-target-experience | adobe-target-content | |||
---|---|---|---|---|---|---|---|---|---|---|---|
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. |
09/14/2021 |
quickstart |
|
true |
DocsExp–386541–A/B–Enhanced-Readability-Quickstarts–2.19.2021 |
Experience B |
./create-first-function-cli-csharp-ieux |
In this article, you use command-line tools 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.
[!INCLUDE functions-dotnet-execution-model]
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.
Before you begin, you must have the following:
-
Azure Functions Core Tools version 4.x.
-
One of the following tools for creating Azure resources:
-
Azure CLI version 2.4 or later.
-
The Azure Az PowerShell module version 5.9.0 or later.
-
-
.NET Core 3.1 SDK. Required by the build process.
-
Azure Functions Core Tools version 4.x.
-
One of the following tools for creating Azure resources:
-
Azure CLI version 2.4 or later.
-
The Azure Az PowerShell module version 5.9.0 or later.
-
You also need an Azure account with an active subscription. Create an account for free.
Verify your prerequisites, which depend on whether you are using 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
dotnet --list-sdks
to check that the required versions are installed. -
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.
-
In a terminal or command window, run
func --version
to check that the Azure Functions Core Tools are version 3.x. -
Run
dotnet --list-sdks
to check that the required versions are installed. -
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.
In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function.
-
Run the
func init
command, as follows, to create a functions project in a folder named LocalFunctionProj with the specified runtime:func init LocalFunctionProj --dotnet
func init LocalFunctionProj --worker-runtime dotnet-isolated
-
Navigate into the project folder:
cd LocalFunctionProj
This folder contains various files for the project, including configurations files named local.settings.json and host.json. Because local.settings.json can contain secrets downloaded from Azure, the file is excluded from source control by default in the .gitignore file.
-
Add a function to your project by using the following command, where the
--name
argument is the unique name of your function (HttpExample) and the--template
argument specifies the function's trigger (HTTP).func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
func new
creates a HttpExample.cs code file.
If desired, you can skip to Run the function locally and examine the file contents later.
The function code generated from the template depends on the type of compiled C# project.
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).
HttpExample.cs contains a Run
method that receives request data in the req
variable is an HttpRequestData object that's decorated with the HttpTriggerAttribute, which defines the trigger behavior. Because of the isolated process model, HttpRequestData
is a representation of the actual HttpRequest
, and not the request object itself.
:::code language="csharp" source="~/functions-docs-csharp/http-trigger-isolated/HttpExample.cs":::
The return object is an HttpResponseData object that contains the data that's handed back to the HTTP response.
To learn more, see Azure Functions HTTP triggers and bindings.
-
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 ...
[!NOTE] If HttpExample doesn't appear as shown above, 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.
-
Copy the URL of your
HttpExample
function from this output to a browser:To the function URL, append the query string
?name=<YOUR_NAME>
, making the full URL likehttp://localhost:7071/api/HttpExample?name=Functions
. The browser should display a response message that echoes back your query string value. The terminal in which you started your project also shows log output as you make requests.Browse to the function URL and you should receive a Welcome to Azure Functions message.
-
When you're done, use Ctrl+C and choose
y
to stop the functions host.
[!INCLUDE functions-create-azure-resources-cli]
-
Create the function app in Azure:
az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location <REGION> --runtime dotnet --functions-version 3 --name <APP_NAME> --storage-account <STORAGE_NAME>
The az functionapp create command creates the function app in Azure.
az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location <REGION> --runtime dotnet-isolated --functions-version 3 --name <APP_NAME> --storage-account <STORAGE_NAME>
The az functionapp create command creates the function app in Azure.
New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccount <STORAGE_NAME> -Runtime dotnet -FunctionsVersion 3 -Location '<REGION>'
The New-AzFunctionApp cmdlet creates the function app in Azure.
New-AzFunctionApp -Name <APP_NAME> -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccount <STORAGE_NAME> -Runtime dotnet-isolated -FunctionsVersion 3 -Location '<REGION>'
The New-AzFunctionApp cmdlet creates the function app in Azure.
[!NOTE] This command creates a function app using the 3.x version of the Azure Functions runtime. The
func azure functionapp publish
command that you'll run later updates the app to version 4.x.In the previous example, replace
<STORAGE_NAME>
with the name of the account you used in the previous step, and replace<APP_NAME>
with a globally unique name appropriate to you. The<APP_NAME>
is also the default DNS domain for the function app.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.
[!INCLUDE functions-publish-project-cli]
Because your function uses an HTTP trigger and supports GET requests, you invoke it by making an HTTP request to its URL. It's easiest to do this in a browser.
Copy the complete Invoke URL shown in the output of the publish command into a browser address bar, appending the query parameter ?name=Functions
. When you navigate to this URL, the browser should display similar output as when you ran the function locally.
Copy the complete Invoke URL shown in the output of the publish command into a browser address bar. When you navigate to this URL, the browser should display similar output as when you ran the function locally.
[!INCLUDE functions-streaming-logs-cli-qs]
[!INCLUDE functions-cleanup-resources-cli]
[!div class="nextstepaction"] Connect to Azure Queue Storage
[!div class="nextstepaction"] Connect to Azure Queue Storage