DEV Community

Cover image for Dependency Injection in Azure Functions
Surjyo Bhattacharya
Surjyo Bhattacharya

Posted on • Originally published at surjyob.hashnode.dev

Dependency Injection in Azure Functions

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.

static key words

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.

Interface

We have defined an Interface that ensures the contract CreateBlob is to be established.

Class Implementation

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;

        }

Enter fullscreen mode Exit fullscreen mode

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

  1. microsoft.azure.functions.extensions

Nuget Package

  1. azurefunctions.extensions.dependencyinjection

Nuget Package

Next, we shall add the Startup.cs class in the Utility project as follows

Startup class

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 dependency injections 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 :

Function App

On building the application and running we get the following, and we are already with the HttpTriggered function.

Running Function

Reference : https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection

Top comments (0)