Introduction to Azure Functions

Azure functions are an event driven app, compute on-demand experience to execute a code based on the various events occurring in Azure, 3rd party and on premise environment. Dynamic compute is one of the prominent feature in azure functions, where developers no need to worry about the infrastructure. Azure allocates the computing resource dynamically and span across the azure infrastructure whenever high computing power is required, scale up and scale down automatically. This is called as “Serverless computing”.

function1

Developers can write code in their favorite languages (C#, Node.js, Python, F#, PHP, batch, bash, Java, or any executable). Developers can write the lightweight code in the azure portal itself instead of choosing the development platform (VS etc.).  Azure developers can use the declarative feature in portal to write the functions and binds to the events, adding the inputs to the functions and define the output of the function. 

Azure Functions Runtime

Azure Function runtime is built on top of the Azure webjobs sdk. Declarative code attributes are captured in JSON file. This metadata used to identify the programming languages and determine the .NET SDK jobs sdk runtime. The runtime, otherwise known as the script host, is the underlying Webjobs SDK host which listens for events, gathers and sends data, and ultimately runs your code. Function.json metadata file describes the bindings, along with a collection of one or more script files implementing the function

Folder Structure

You can edit the function in Azure Portal using the Visual Studio online extension.

function2

parentFolder (for example, wwwroot in a function app)

| – host.json

| – addEmployee

| | – function.json

| | – run.csx

The host.json file contains some script host specific configuration and sits in the parent folder

Functions.json –  defines the bindings. And if you want to prevent the function to be executed , you can set the disable to true.
Business Scenarios and Azure Functions
There are many business scenarios we can use azure functions with less complexity of code.

  1. Long running task.
  2. Polling a resource, based on status and execute a task.
  3. Multiple threads execution
  4. Execute a task based on the events (Event driven)

The above scenarios can be easily achieved through azure functions with the out-of-box template provided by the platform.

  • Blog Trigger – execute a code when there is a blob added to the storage or containers.
  • Queue Trigger – respond to trigger when a new item is added.
  • Event Hub Trigger – respond to the trigger when an event added to event hubs. (IoT Scenarios)
  • Generic WebHooks – Functions can be a service and process the http request when there is webbook event raised.
  • HttpTrigger – Trigger the execution of your code by using an HTTP request
  • ServiceBusTrigger – Connect your code to other Azure services or on-premise services by listening to message queues.
  • TimerTrigger – Execute cleanup or other batch tasks on a predefined schedule

Walkthrough an example
I have a simple scenario, add a new employee to the HRMS system when the new employee joined the organization.  To make it simple, I have a console application which feeds the employee information to the azure queue and the azure functions process the queue message and the employee information to the SQL Azure.

function4

The above code looks very simple.

  1. Run is the method will be executed when the queue event triggered.
  2. Second line of the code, deserialze the “queue” messages into the employee object.
  3. Calling an another function “AddToEmployee” insert the records into the database.
  4. Log the information.

Developer Notes:

  1. Developers can call any .net libraries. The same libraries should be referred in the code as directives. Refer the first three lines. Use “#r” symbol to refer .net libraries.
  2. In Azure functions, developers write the C# script (which is same as C#) but executed as scripts in the environment. Refer my other blog to see “C# script”.
  3. Use the using statement, to access those libraries in the code.
  4. By default, the following namespace are available as part of the execution environment.
  • System
  • Collections.Generic
  • IO
  • Linq
  • Net.Http
  • Threading.Tasks
  • Azure.WebJobs
  • Azure.WebJobs.Host
  1. To refer the external assembly, use the “#r” “Assembly name” directive.
  2. To use NuGet packages in a C# function, upload a project.json file to the the function’s folder in the function app’s file system.

Example: To refer the CRM NuGet Package.

{

“frameworks”: {

“net46”:{

“dependencies”: {

“Microsoft.CrmSdk.CoreAssemblies”: “7.1.0”,

“Microsoft.CrmSdk.Deployment”: “7.1.0”,

“Microsoft.CrmSdk.Extensions”: “7.1.0”

}

}

}

}

  1. Reusing the CSX code à developer can load the. csx code through #load utilities.csx.  add the. csx code in the functions folder. Use the relative path to refer and load the csx files if you placed. csx in different folder.

Examples:

#load “utilies.csx”

#load “utilities\utilities.csx”
Hosting Plan – What to choose?
As azure developer, it’s very important to know about the hosting plan. There are two ways to host this function.

  1. App Service Plan –  Azure functions will be hosted in a dedicated VM same as web apps today. The azure functions will be available at all time, whether the code is executing or not.

Scenarios to choose this plan.

  • If you have web apps with less utilization.
  • If your functions are going to execute continuously or almost continuously.
  1. Dynamic Plan –  Developers no need to worry about the resource management. Azure dynamically allocate the computing resources. Those computing resource can span across the azure infrastructure. And also, azure functions can run in parallel to minimize the total time to minimize the process request.

Scenarios to choose this plan:

  • When try to speed up the processing using multiple threads (Parallel processing).
  • One-time execution.
  • When require, Dynamic scaling.

The hosting plan can be defined in the azure function portal or through JSON declaration.