Microsoft Azure Functions the Future of Business Computing
This article provides a quick high-level overview of Microsoft Azure Functions with a practical example of how to create, compile and debug an Azure Function using Visual Studio 2017 v15.3, using BlobTrigger function type & C# as language.
For novice Cloud developers, aspiring Cloud architects, Pre-sales consultants, and all other IT professionals associated with the Cloud computing, especially with Microsoft Azure, this tutorial would explain working of Microsoft Azure Functions with Visual Studio 2017 v15.3
Azure Functions is Microsoft Azure’s offering for Serverless Architecture model. Azure Functions is one of the most used solution for running small pieces of the code or 'functions' in the Cloud seamlessly. Visual Studio 2017 v15.3 along with .NET Core 2.0 release, comes up with the in-built feature of Azure Functions development. With a predefined set of Azure Function types like, BlobTrigger, HttpTrigger and many more. Microsoft has made developer to concentrate more on productivity with providing easier templates & code samples.
Let’s understand with an example.
1) Microsoft Azure Subscription (You can have your free trial subscription, by clicking, https://goo.gl/Uu9KdL )
2) .NET Core 2.0 - Download at https://goo.gl/GWrvoB
3) Visual Studio 2017 V15.3 Preview - Download at https://goo.gl/3omSCF
4) Azure Storage Explorer (For testing purpose)
Azure Function Type – BlobTrigger
Open Visual Studio 2017 v15.3 and Click on File => New Project
Select Cloud under Visual C# option in Left pane, and select Azure Functions.
Provide application a meaningful name. Here we have entered name as ‘BlobFunction’.
Click OK, to proceed.
This will create an application with basic project structure, with minimal required assembly references, .NET SDK for functions along with two json file naming host.json & local.setting.json.
As we are working with BlobTrigger Azure Function Type, we need to have Azure Storage account. Basically this azure function type, executes a piece of code, when any file is being added to Blob container. This inclusion of files triggers an event. Hence named as BlobTrigger. You can run any use-case or workflow, on this trigger.
In this article we will go with default code execution created as part of template.
So now, as we need to implement BlobTrigger azure function type, connecting our application with Storage Account would be required. This would be achieved by adding Azure Storage account connection string in to our application.
Navigate to below shown section in Azure Portal under your Storage Account and Click on Access Key pane.
Copy one connection string from among Key1 or Key2, as shown.
Now coming back to application in our Visual Studio 2017 v15.3, open up ‘local.settings.json’ file, and paste the copied connection string as value for predefined Key, AzureWebJobsStorage.
Refer below image.
Under Solution Explorer, right click on project => Add => Select New Item
Select ‘Azure Functions’, provide function name and Click Ok. Here we have given function Class file name as ‘blobfun’
This will open window with different Azure Function Types to be develop.
Select Blob Trigger.
It will open up for entering two more details,
Connection: Provide Key name, ‘AzureWebJobsStorage’ from local.setting.json file. This will consider as connection string to get connect with Azure Storage.
Path: This is the Blob container name. Adding file in this container will trigger the function. Here we have given container name as ‘samples-workitems’, it’s a default name. We can provide any name of our choice. Make sure container with same name do exist in your Azure Storage Blob.
This will create a class file name, BlobFun.cs with sample code to execute once any file is being added. It comes with FunctionName attribute an entry point for Azure Function, along with sample code.
If any file is uploaded in blob container, it will trigger this function. This Code will read file and display few details like name of file and size of file in bytes.
Put a debugger point at code as shown in below image, for debugging Azure Functions in Visual Studio 2017.
Click F5 to run the application.
This will open a console window, listing all details.
Will hold with displaying message, ‘Job Host started’, our function is now running.
Testing Azure Function Locally
Let’s test this function, using Azure Storage Explorer, a client desktop application for managing Azure Storage. Managing blob containers, tables, queues and files, becomes much easier using this explorer.
You can very well use other options like through code or portal but for easier implementation and being most recommended option, we will be using Azure Storage Explorer.
Navigate to ‘samples-workitems’ Blob container, and click on Upload button, as shown in below image.
Browse any file for uploading, here we are uploading Image file name, ‘dot-net-core-2.0.jpg’
Click on Upload.
This will upload file in our Blob container, as seen below.
Once file is uploaded, this will trigger Azure function.
And here comes the pointer at our debugger point.
Notice, it display the name of file and file size.
File name as ‘dot-net-core-2.0.jpg’
And, File size as ‘11046’ bytes.
Click F5 or continue, and the same results can be seen in running console window.
Our function is running absolutely fine locally.
But for transforming it into an action we need to publish our Function on Cloud i.e. Azure Functions app.
Publishing to Azure Functions using Visual Studio 2017
Along with developing and debugging Azure Functions, we can also publish this functions using Visual Studio 2017 v15.3
Open Solution Explorer, Right Click on project name and Click on Publish.
This will open a window with three different publish options under Publish tab,
· Azure Function App
· Import profile
In this article, will go with Azure Function App as our publishing option.
Select Azure Function App and Create New,
Click on Publish.
The most important step, comes with this window, wherein you have to fill in all required details which would create your Azure Function app on Azure. Make sure you are logged in with your Microsoft Azure account with subscription.
· App Name: Is a globally unique Function App name. It suggest with default name for App. Let’s go with default name.
· Subscription: It will list your subscription associate with your logged in account. If you have multiple subscription, you can select any one from it.
· Resource Group: You can select existing Resource group or create new.
· App Service Plan: As it’s a Function App we can select consumption plan wherein it will charge only on execution of code.
· Storage Account: You can create a new storage account or select from existing storage accounts.
Tip: Better to create Storage account from Azure portal and then select the same from this window.
Once done with filling in all required details, click on Create.
This will start publishing / deploying our function application to Azure Function App.
Further it will lead to window, displaying Azure Function app details, like URL, its configuration detail as Release, username and password.
It also comes up with an option to rename or delete function profile.
Once ‘Publish’ button turns enable, our deployment succeeds.
You can open site URL in browser, once Function app is deployed, it displays message,
‘Your Function App is up and Running’
Now, open Azure portal, http://portal.azure.com/
Click on Resource Group we selected, to check our Function App.
Click on Functions Apps.
Newly published Function will be displayed along with details like its subscription, Resource group and Location it’s being deployed.
Click on our Function App listed.
Our application ‘blobfun’ would be listed under Function Tab.
Details like connection, path can be seen here, as highlighted in below image.
Let’s repeat the above Testing steps of uploading an image from Azure Storage Explorer, at mentioned blob container.
Check logs section, details,
File name and File size are displayed.
Visual Studio 2017 v15.3 made getting started with developing serverless Azure Functions much easier for developers. Would highly recommend to give a try for this blob trigger function type, making your application more interesting with lesser price consumption.
This article is shared by asp.net developers to explain the .net technology. If you have any doubt or want to know more about the technology, kindly comment below.
A : "I have written over 100,000 lines of code in primary school" B : "Oh. So cool. Then what's after?" A : "Then I learnt how to write loop statements"