layout | title | nav_order |
---|---|---|
default |
Task 1 |
1 |
The Azure Functions extension in VS Code lets you create a function app project, along with your first function.
-
In VS Code, under the
Workspace
section of the Azure extension, selectAdd
andCreate Function
. -
You will now be given the option of creating a function project, select yes.
Going forward you will not be prompted for configurations for the project. Input the following values:
-
Language: C#
-
Template for first function: HTTP trigger
-
Function name: HttpTriggerFunction
-
Namespace: LearningFunctions.HttpTriggerFunction
-
Access rights: Anonymous
Your project and first function is now being set up and a number of files will be created in the folder.
Your workshop folder should look like this
A number of files are generated, for the most part they will be left as is, but there are two files you will need to edit.
-
local.settings.json
holds the settings for running the function locally and you will be updating this later in the workshop. -
HttpTriggerFunction.cs
contains the function.
[FunctionName("HttpTriggerFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
The functions first input parameter has an attribute [HttpTrigger(...)]
that specifies that the function response to an http trigger
.
The function supports both HTTP GET
and HTTP POST
requests, no authorization is required and no route is defined.
The input is an HttpRequest
object and an ILogger
object.
Each time the function is triggered, a text string is written to the console and a text string is returned to the client.
Question
Which text string will appear in the console and which text string will appear in the browser?
Not quite sure? Go on to the next step to find out!
The auto created function is ready to run as is, let's test that everything is set up ok.
- Open a new terminal in VS Code
- Navigate to the project folder and run cmd
func start
If everything is successful the output in the terminal should look like this
Navigate to the url displayed in the terminal to see your function.
Run the cmd func start
after each step to see the results.
-
Hard coded response.
In the case no name is passed to the function, change the text in the response from
This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
toWelcome to my first Azure Function!
-
Accessing query parameters
The function template retrieves a single query parameter name. Can you extend the function to process two query parameters
name
andcompany
?Use the parameters to print a response to the user in the following format:
Hello, [name]. Are you ready for this workshop with [company]?
Hint: To send a query parameter to the function add
?{parameterName}={parameterValue}
at the end of the url. E.g.http://localhost:7071/api/HttpTriggerFunction?company=Avanade
If you want to send multiple parameters separate them with&
. -
Returning dynamic data
Instead of printing a hard coded string, can you make the function return the current time?
Hint: In C#, the DateTime object is defined in the
System
namespace. To get the current date useSystem.DateTime.Now
.
Question
During the configuration of the function we set the access rights to anonymous
.
Other available options are Function
and Admin
when would you use these two configurations?