Dependency Injection in Azure Functions
Table of contents
What is Dependency Injection?
As per Wikipedia :
In software engineering, dependency injection is a design pattern in which an object or function receives other objects or functions that it depends on. A form of inversion of control, dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function which wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' (object or function) is provided with its dependencies by external code (an 'injector'), which it is not aware of.[4] Dependency injection helps by making implicit dependencies explicit and helps solve the following problems:[5]
How can a class be independent of the creation of the objects it depends on?
How can an application, and the objects it uses to support different configurations?
How can the behavior of a piece of code be changed without editing it directly?
We intend to use the same IOC behavior in using the Azure Functions implementing the Dependency Injection principle.
Demo Function with DI
For this let us write a demo function as seen below.
Notice that by default the Function1 class is static as well as the Function is also defined as static.
Also, a point to note is - Support for dependency injection begins with Azure Functions 2.x. The guidance in this sample would work in the case of C# class library functions that run in-process with the runtime.
Considering the above points, let us add a class library utility as seen below.
We plan to interact with the Azure Blob Storage account via the Azure Function and hence adding the Blob Service instantiation-related functionality to the Utility and in the main Azure Function shall not be required to manage the connections of the Azure Blob.
We have defined an Interface that ensures the contract CreateBlob is to be established.
We have defined the implementation class: BlobStorageManagerService which has implemented the CreateBlob. Please note the section of the code below :
private readonly string _blobConnectionString;
private readonly BlobServiceClient _blobServiceClient;
public BlobStorageManagerService(string blobConnectionString)
{
_blobConnectionString = blobConnectionString;
BlobServiceClient blobServiceClient = new BlobServiceClient(_blobConnectionString);
_blobServiceClient = blobServiceClient;
}
The above code is an example of the implementation of DI, where the blobServiceClient is defined in the constructor of the BlobStorageManagerService.
Next, we will need to inject the dependencies at the startup, for that we shall need to add the following 2 Nuget Packages
microsoft.azure.functions.extensions
azurefunctions.extensions.dependencyinjection
Next, we shall add the Startup.cs class in the Utility project as follows
Note the sections in red, we have to use the above convention in the Startup to ensure that when the Utility is used as a reference to the original function app project, the proper dependencies are injected and the rest of the code can run smoothly thereafter.
Finally, in the DemoFunction App, we shall be implementing the DI code as follows :
On building the application and running we get the following, and we are already witht the HttpTriggered function.
Reference : https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection
Subscribe to my newsletter
Read articles from Surjyo Bhattacharya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Surjyo Bhattacharya
Surjyo Bhattacharya
Integration Architect specializing in Azure Integration Services and BizTalk. Multi-cloud exposure with AWS. C# and SQL development experience as part of developing several projects