Azure Functions, bits and pieces
While working with Azure Functions, I found my self trying to handle some important points and I will list here the solutions.
Dependency Injection
When creating an Azure Function via Visual Studio, they come with the static modifier. To enable dependency injection, you will need to remove it and add a class named Startup.cs prefixed with the following attribute:
When referencing the injector, using a .NET Core like syntax, one will need to use the “builder.Services” property:
Follows the full example:
CORS
CORS is a security mechanism in place to establish if a browser can allow an application to access a resource hosted on a different domain from the one is running on:
It is possible to set CORS easily from the “local.settings.json” file as shown below:
To set CORS on Azure instead will be possible by clicking on “Platform Features”:
Then on the CORS list item:
Then add “*” to allow more origins:
Limiting the number of requests
In general, Azure Functions are a tool handy when someone is looking for scalability. They may suffer from cold starts, but they are perfect for unpredictable volumes of requests.
There is no way to limit the number of requests served by a function. The best way I could think of was by implementing the following architecture:
Placing API management in front of your functions could allow making the best out of the two.
Conclusions
Azure Functions flexibility allows them to be used in many different contexts