Dependency Injection in Azure Functions
Tags: azurefunctions, dependencyinjection, azure, csharp
Published: 2022-12-04 18:30:00

## What is Dependency Injection?

As per [Wikipedia ](https://en.wikipedia.org/wiki/Dependency_injection):

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuc9to6i9crtqvxixjfy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uspl85ywkoav1evzsyfg.png)

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


![Class Implementation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjxcfdwjwi6p0cb82xji.png)

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


1. microsoft.azure.functions.extensions


![Nuget Package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9k03nd0ige71kbul162p.png)

2. azurefunctions.extensions.dependencyinjection


![Nuget Package](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c823pazzzh8aj9u5yg0.png)

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


![Startup class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj5o9i466aal6c9x8uws.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdprnj7typy855ixiyma.png)

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


![Running Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oq5xno2me5xavsp07ou.png)

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