Introducing Azure Functions

This article will show you how to create a new Azure Function that will expose an HTTP endpoint which will accept a couple of Query String parameters from the HTTP Request, and then will output a calculated value based on the input parameters

Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure. Use Azure Functions to run a script or piece of code in response to a variety of events.

This article will show you how to create a new Azure Function that will expose an HTTP endpoint which will accept a couple of Query String parameters from the HTTP Request, and then will output a calculated value based on the input parameters.

Here we want to create a Function that is triggered by an HTTP endpoint.

Step 1

Login to your Microsoft Azure portal, click on Create new resource and select Function App from Compute category. Enter the Name, what Subscription you want it to belong to, create a new Resource Group, choose the hosting plan as Consumption Plan, the Location where we want to host the same and leave the Storage account as default (you can customize the name or select an existing according to your choice). Wait for a few minutes for the resource to be created. 

Azure

Step 2

Once the deployment is succeeded, open the resource and click the Plus (+) next to the Functions node underneath the Azure Function App. This will bring up an interface to initiate the creation of a new Function within the app.
 
Azure 

Step 3

Click on the Custom function link which you can find at the bottom of the Get started quickly interface.

Azure 

Step 4

Select the HttpTriggerWithParameters C# Template from the Choose a template interface. This will setup a new Function that can be triggered over an HTTP endpoint and accept Query String parameters sent within the HTTP Request. It will also be setup to output to the HTTP Response. Make sure you select the C# Version.

Azure 

Step 5

Leave the Language as C#, give a Name or go with default, leave the Authorization level function as default and click on default which will create the new function based on the template. Now we have just created a new function that is triggered by an HTTP endpoint.

Azure 

Step 6

Here, the default template function will accept a single Query String parameter with the name of the HTTP Request. Then the function prefixes the passed value with Hello and returns the result in the HTTP Response.

Azure 

Step 7

Click on the Run button above the code to open the Test Pane view to test the functionality of this function.

Azure 

Step 8

In the Test pane to the right of the code view, enter a value in the value field underneath Query next to the name parameter and set GET for the HTTP Method and click run again with the configured setting and parameter (If it is showing status code error as unauthorized, go to manage blade of the function and click on renew for the function key)

Azure 

Now triggering of the Function ran successfully with an HTTP Status code of 200 OK showing result of the Function that was returned via the HTTP Response of the Function.

Step 9

Now we will modify the Azure Function to accept 2 numbers as parameters and then return the result of multiplying those 2 numbers together. In the Run method remove the string name parameter and replace the return statement with the following code snippet that multiplies the 2 parameters together (one and two), then returns the result in the output of the method to send it to the HTTP Response. Click Save to save the code file.

  1. var val1 = double.Parse(req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "val1"true) == 0).Value);  
  2. var val2 = double.Parse(req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "val2"true) == 0).Value);  
  3. var sum = val1 + val2;  
  4. return req.CreateResponse(HttpStatusCode.OK, sum); 

 

Azure

Step 10

In the pane to the right of the code view, click on the View files tab, open the function.json which holds the json configuration for the Function and replace the contents of the function.json file with the following code snippet

  1. {  
  2.     "bindings": [{  
  3.         "authLevel""function",  
  4.         "name""req",  
  5.         "type""httpTrigger",  
  6.         "direction""in",  
  7.         "methods": ["get"]  
  8.     }, {  
  9.         "name""$return",  
  10.         "type""http",  
  11.         "direction""out"  
  12.     }],  
  13.     "disabled"false  

 

Azure

Step 11

Go back to the Test tab and modify the Query parameters by removing the name parameter and adding 2 parameters named val1 and val2 with numerical values.

Azure

Step 12

Click the Run with two values in the value field and validate the Output of the method and the result that is displayed

Azure