Using Constructor Injection to Implement DI in Microsoft Azure

Using Constructor Injection to Implement DI in Microsoft Azure

Before we look into implementation of DI in Azure functions, we shall look into what dependency injection is and its advantages.

What is dependency injection (DI)?

Dependency Injection is a design pattern that helps achieve loosely coupled codes. Dependency Injection is a technique to achieve Inversion of Control (IoC) between classes and their dependencies.

What are the advantages of dependency injection?

To build large applications, It is required to make our code more maintainable, reusable, more readable, scalable and testable. DI makes unit testing easier as we can mock dependencies. So, using DI we can achieve all these things and in this article we will look at how to implement in Azure functions.

Azure Functions is a server-less offering from Microsoft Azure (similar to Lambda function in AWS) to concentrate on solving business problems rather than maintaining infrastructure.

To understand the DI, We will implement dependency injection (DI) pattern in Azure functions v2 and create a simple HTTP trigger function which downloads the blob data from the Azure blob storage (We will use Azure storage emulator for blob storage).

Getting started

Let us create a new azure function project using Visual studio 2019 (IDE) as we cannot implement dependency injection using the Azure portal.

New project

After selecting the Azure functions as shown in the above screenshot, you can click on the Next button to configure new project with name. 

configure new project

Let’s name the project as “MyFunctionApp”. You can give any relevant name to your project. After entering the project name and other details you can click on the Next button to create the HTTP trigger as below screenshot.

new azure functions

As we are going to create Azure functions v2, we have to select the option and then select Http Trigger as shown in the above screenshot.

Now, the configuration has been done and once we click on the “Create“button it will create a sample Http trigger function with code files as below screenshot.

solution explorer

Now, we are ready to implement the DI in our Azure function and to do that, we need to create an interface and a class which has an implementation of our interface method(s).

IStorageService.cs

StorageService.cs

We have now created the IStorageService interface and StorageService class as shown in the code samples above. Now, we need to create a Startup assembly file that initiates the build object to register the StorageService object with IStorageService interface.

We will need to include below nuget package:

  • Microsoft.Azure.Functions.Extensions 1.1.0

After installing the mentioned package in the project, we can create a Startup class to register our service with service lifetime in the root of the project. You should have the below code sample to register the service with IoC container. Here we provide a delegate to manually instantiate the storage service.

Startup.cs

Service lifetimes

In the above code sample, I have registered a service with AddSingleton method to create an object of service once in service lifetime. 

You can register our service with 3 different service lifetime methods:

  • AddSingleton – objects are the same for every object and every request.
  • AddScoped – objects are the same within a request, but different across different requests.
  • AddTransient – objects are always different; a new instance is provided to every controller and every service.

Now, we can inject our service in the constructor to the HttpTrigger function and use its method to download the blob data from the Azure blob storage. In this case, we have used an Azure storage emulator to download blob data.

Function1.cs

When we create an azure function with a default template it creates a function class with a static keyword. 

If you look at the above Azure function code, I have removed the static keyword before the class name. Because we need to add a constructor of class to inject our service dependency, registered in service builder class in the Startup.cs file. Note: Static classes cannot have constructors. 

We have declared a read only private property for the IStorageService Interface in the Constructor that will initiate the StorageService in the runtime. The IoC container will inject the correct implementation of the interface based on the setup in the Startup class.

Now our code is ready to test!

Azure storage emulator:

Azure storage emulator

Results

You can build and run the project. In my case, It runs with the below URL to download the blob from Azure blob storage.

http://localhost:7071/api/DownloadBlob?containerName=profile-images&fileName=azure-developer-associate.png

In this request to download the blob data, I have passed the containerName and fileName parameter as a query string which we want to download. 

Here is the actual request and response in the Postman:

response in the Postman

That’s it for dependency injection in Azure functions using C#. In case of any doubts, do not hesitate to get in touch with an expert at DEV IT here.

The following two tabs change content below.

Haresh Hanat

Sr. Software Developer(.Net) at Dev Information Technology Ltd.
Haresh Hanat is a computer engineer by education and a senior software developer by profession. With over seven years of experience as a software engineer, Haresh is an expert at .NET, MEAN stack, and cloud technologies. During his time of leisure, Haresh likes to play cricket. He is a Microsoft certified Azure cloud developer with exceptional problem solving and grasping skills. He aims to develop cutting-edge software solutions in the future to come!

Latest posts by Haresh Hanat (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *