In this article, we are going to learn about the Azure function and see a small basic demo.
What Are Azure Functions?
Use of Azure Function
The Azure Functions are a lightweight and serverless compute service. You can’t use the Azure function with a larger website.
- Use Scheduled Tasks.
- Use for Notifications and Reminders.
- Use to build a lightweight web API.
- Use to send background emails.
- Use to run backup tasks in the background.
Azure functions are best suited for small apps with events that can function independently of other websites. Sending emails, starting the backup, order processing, task scheduling such as database cleanup, sending notifications, messages, and IoT data processing are some of the common Azure functions.
Assume you need to send a birthday email to each of your customers. You work as a web developer in ASP.NET. Instead of creating an ASP.NET website, deploying it, and hosting it on IIS just for one feature, you can simply write an azure function, insert your email login, and deploy it on the Azure cloud. The Azure functions will connect to your data source directly, retrieve your customers’ emails, and send them an email on a predetermined date and time.
Advantages of Azure Function
- The Azure Functions app is serverless and does not require the installation of a Web server in the cloud.
- The Azure functions app is small and takes few resources to launch and run.
- No resources consume when it is not in use, the Azure functions app is compute-on-demand.
- The Azure functions app is event-driven, meaning it only runs when an event occurs.
- It’s simple to create and deploy an Azure functions app.
- You don’t have to pay anything if you’re not using the Azure functions app.
- The Azure functions app is simple to use and maintain.
Let’s start with an example.
Open your visual studio in 2019 and search Azure Functions and click on the Next button.
Then give Project Name and provide the location where you want to locate your project and then click on Create button.
After that below screen will display then you have to choose the HTTP trigger option and then click on Create button.
Finally, your azure function is created and you can see a couple of codes in it as shown below image.
When a function trigger is executed, the Azure Functions runtime calls the Run method. The Run method’s first argument is of type HttpRequest. It also has a HttpTrigger attribute. This is the C# equivalent of telling Azure Functions that the function should be executed whenever the HttpTrigger is executed.
The Run method’s second argument is an ILogger. When working with Azure Functions, logging is very useful because you often have a set of different Functions to build a solution, and you need to make sure you understand what happens or does not happen in your system.
The method’s implementation looks for a name query argument and writes a response message if it is found in the HTTP request.
Now let’s run your project.
Oops, I got this error are you getting the same error?
No need to worry about it we just want to start the Azure Storage Emulator on our PC.
Run the following command in it.
AzureStorageEmulator.exe start -inprocess
Now let’s again try to run your project.
Yeah, now it’s working fine then run this URL in the browser.
You can also pass your name as a parameter and see output like this.
Azure Functions is a serverless, compute-on-demand, lightweight, and self-contained application. We learned what the Azure Functions app is, what its benefits are, and when to use it. We also learned how to use Visual Studio to construct an Azure Functions app.
In my next article, we are going to learn How To Publish Azure Function From Visual Studio