Whenever I meet clients and give a talk about Azure Functions, they are immediately interested in replacing their existing Web API features with Azure Functions. In this post, I’d like to discuss:
- Can Azure Functions replace Web API?
- Is it worth doing?
It would be a good idea to have a read through this article, Serverless Architectures, before starting.
HTTP Trigger Function == Web API Action
One of the characteristics of Serverless Architecture is “event-driven”. In other words, all functions written in Azure Functions are triggered by events. Those events, of course, include HTTP requests. From this HTTP request point of view, both HTTP trigger function and Web API action work exactly the same way. Let’s compare both codes to each other:
How do both look like? They look pretty similar to each other. Both take an HTTP request, process it and return a response. Therefore, with minor modification, it seems that Web API can be easily migrated to Azure Functions.
HTTP Trigger Function != Web API Action
However, life is not easy. There are some major differences we should know before migration:
Functions are always static methods
Even though Azure Functions are extensions of Azure WebJobs, each function has a static modifier by design, unlike Azure WebJobs can be without the static modifier.
Actions of Web API, by the way, don’t have the static modifier. This results in a significant architectural change during the migration, especially with dependency injection (DI). We will touch it later.
Functions always receive HttpRequestMessage
instance as a parameter
Within the HTTP request/response pipeline, a Web API controller internally creates an HttpContext instance to handle data like headers, cookies, sessions, querystrings and request body (of course querystrings and request body can be handled in a different way). The HttpContext instance works as an internal property so any action can directly access it. As a result, each action only passes necessary details as its parameters.
On the other hand, each function takes a different HTTP request/response pipeline from Web API, which passes the HttpRequestMessage instance to the function as a parameter. The HttpRequestMessage instance only handles headers, querystrings and request body. It doesn’t look after cookies or sessions. This is the huge difference between Web API and Azure Functions in terms of stateless.
Functions define HTTP verbs and routes in function.json
In Web API, we put some decorators like HttpGet, HttpPost, HttpPut, HttpPatch and HttpDelete
on each action to declare which HTTP verbs take which action, by combining with the Route decorator.
On the other hand, each function has a definition of HTTP verbs and routes on function.json
. With this definition, different functions having the same route URI can handle requests based on HTTP verbs.
Functions define base endpoint URI in host.json
Other than the host part of URI, eg) https://api.myservice.com, the base URI is usually defined on the controller level of Web API by adding the Route
decorator. This is dead simple.
However, as there’s no controller on Azure Functions, it is defined in host.json
. Default value is api, but we can remove or redefine to others by modifying host.json
.
While function.json
can be managed at the function level through GUI or editor, unfortunately it’s not possible to edit host.json
directly in the function app. There’s a workaround using Azure App Service Editor to modify host.json
, by the way.
Functions should consider service locator pattern for dependency management
There are many good IoC container libraries for Web API to manage dependencies. However, we have already discussed this in my previous post, Managing Dependencies in Azure Functions, that Service Locator Pattern should be considered for DI in Azure Functions and actually this is the only way to deal with dependencies for now. This is because every Azure Function has the static modifier which prevents us from using the same way as the one in Web API.
We know different opinions against service locator patterns for Azure Functions exist out there, but this is beyond our topic, so we will discuss it later in another post.
Is Azure Functions over Web API Beneficial?
So far, we have discussed what are the same and what are the differences between Web API and Azure Functions HTTP Trigger. Back to the initial question, is it really worth migrating Web API to Azure Functions? How does your situation fall under any of below?
- My Web API is designed for microservices architecture: Then it’s good to go for migration to Azure Functions.
- My Web API takes long for response: Then consider Azure Functions using empty instance in App Service Plan because it costs nothing more. Consumption Plan (or Dynamic Service Plan) would cost too much in this case.
- My Web API is refactored to use queues: Then calculate the price carefully, not only price for Azure Functions but also price for Azure Service Bus Queue/Topic and Azure Storage Queue. In addition to this, check the number of executions as each Web API is refactored to call one Http Trigger function plus at least one Queue Trigger function (two executions in total, at least). Based on the calculations, we can make a decision to stay or move.
- My Web API needs a significant amount of efforts for refactoring: Then it’s better to stay until it’s restructured and suitable for microservices architecture.
- My Web API is written in ASP.NET Core: Then stay there, do not even think of migration, until Azure Functions support ASP.NET Core.
To sum up, unless your Web API requires a significant amount of refactoring or written in ASP.NET Core, it surely is worth considering migration to Azure Functions. It is much easier to use and cost-effective solution for your Web API.