Note: This lab is still under development
[TBD]
- Visual Studio 2017
- Azure Functions and WebJobs Tools Extension for Visual Studio
- Microsoft Azure account
- Azure Storage Explorer
This lab consists of the following exercises:
- Create an Azure Function App
- Create a Blob Trigger Function
- Create a Blob Container with Azure Storage Explorer
- Build Function
- Run and Test Function Locally
- Deploy Function App to Azure
- Create Storage Account
- Configure Published Function App
- Run and Monitor Function
With the Azure Functions and WebJobs Tools Extension for Visual Studio, you can quickly get started with a function app in your local development environment.
- Select File > New > Project to open the new Project dialog.
- In the Project dialog select the C# Azure Functions template, which can be found under the Visual C# > Cloud tree node.
- After typing the desired name and path, click OK.
- In the template dialog, select the Empty template and click OK. [S]
Function Apps host the execution of Functions. We'll use the project that was created in the previous exercise to create a Function. The Blob trigger function template executes when a blob has been added to the blob container.
- In Solution Explorer, found in View > Solution Explorer, right click on the project and select Add > New Azure Function...
- In the New Azure Function dialog, select the Blob trigger template.
- After typing the desired Blob container path, click OK. Save this path name for the next exercise.
The Azure Storage Explorer is a convenient way to access and maintain your storage accounts. The Azure Storage Emulator creates a local storage account that can be used for development and testing without needing to create or deploy a new storage account.
- On the left hand side of the Storage Explorer, select the Local and Attached > Storage Accounts > (Development) > Blob Containers node.
- If the Azure Storage Emulator is not installed, an infobar prompt will appear to install it. Select Download the latest version to download and install the Emulator.
- Right Click on Blob Containers and select Create Blob Container.
- In the container name prompt, set the name as the desired path from the previous exercise.
- Select Add Account to connect to your storage accounts on Azure.
The local development environment is now ready to work with the function. The Azure Functions Extension allows local execution and debugging of Functions in Visual Studio. The provided code has two methods: Run
and Convert
.
-
Run
starts the execution of the function by logging file metadata, confirming that it's a csv before calling theConvert
method to parse the file, and finally printing its contents to the console as JSON. -
Convert
parses the CSV file with the CSVHelper library into a collection, which is then converted to JSON with JSON.NET.
- From Solution Explorer, open the
local.settings.json
file and confirm that the following settings exists. Add this setting if it is missing. These settings will enable the use of the development storage account with the Storage Emulator.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
}
}
- In the same file, add a Connection value with a Connection name of your choice:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"Connection" : "CSVStorage"
}
}
- From Solution Explorer, open the
[YourFunctionName].cs
file. - Add the following libraries to the file with the following
using
statements:
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using CsvHelper;
- Replace the
Run
method with the following
public static void Run([BlobTrigger("to-convert/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
//Only convert CSV files
if (name.Contains(".csv"))
{
var json = Convert(myBlob);
log.Info(json);
}
else
{
log.Info("Not a CSV");
}
}
- Add the following
Convert
method belowRun
:
public static string Convert(Stream blob)
{
var sReader = new StreamReader(blob);
var csv = new CsvReader(sReader);
csv.Read();
csv.ReadHeader();
var csvRecords = csv.GetRecords<object>().ToList();
return JsonConvert.SerializeObject(csvRecords);
}
The code for the function is now complete, and can be run and debugged locally in Visual Studio. The function will run when a file is dropped into the to-convert
blob container, but will only print a JSON version of a file with a .csv extension. During debugging, a console window will appear, containing information on the App and Functions, and text from log.Info
method calls.
- Press F5.
- Drag and drop csv file into
to-convert
blob container from exercise 4.
- Select Build > Publish
- Select Azure Function App and click Publish
- Enter a unique App Name.
- Select a subscription.
- Select existing or create a new resource group with desired name.
- Select existing or create a new app service plan with desired name, location, and size.
- Click Create.
The Function is now published and visible in the Azure portal. In order for it to run as it did locally, it needs a storage account to set the Blob Trigger.
- Select Create a Resource, then search for and select Storage Account.
- Enter a unique Name.
- Choose an Azure subscription.
- Create a new or existing Resource group.
- Select a preferred Location.
- Check Pin to Dashboard.
- Click Create.
- From the portal dashboard, locate and click on the new storage account to open the Overview.
- In the menu to the left of the overview, navigate to Access Keys.
- Copy the connection string.
- In the Storage Explorer, click Refresh All at the top and locate the new storage account.
- Expand the storage account, and select the Blob Container node.
- Right click on the Blob Container node, and click Create Blob Container
- Enter container name as
to-convert
.
- In the portal, navigate to published function. Search for the App name to find it.
- From the function app overview, navigate to application settings.
- At the end of the Application Settings section, click Add new setting.
- Set the name of the setting to the name of the Connection from Exercise 4, and paste the connection string as its value.
- At the top of the applications settings tab, click Save.
The function is now completely configured and can be tested and monitored in the Azure portal.